jsp:getProperty question

K

Ken

I am trying to follow a sun tutorial for creating custom tags. (The
tutorial can be found here if needed: http://java.sun.com/products/jsp/tutorial/TagLibraries16.html
)

The issue:
The documentation here (http://java.sun.com/products/jsp/tags/11/
syntaxref11.fm10.html) states that I _must_ create or locate a bean
with <jsp:useBean> before I use <jsp:getProperty> on that bean. Now
in the Sun example the <jsp:getProperty> is not referencing the bean
from <jsp:useBean> but one from a custom tag.

If this is possible what requirements need to fulfilled so I can make
sure I am fulfilling them? I have done quite a bit of normal Java
programming but having just started with JSPs I'm finding the new
functionality magical.

Hopefully I've provided information but should you need a concrete
example:
========= START EXAMPLE =========
<%--
Document : list.jsp
Created on : 28-Oct-2009, 1:16:43 PM
Author : ken
--%>

<%@ taglib uri="/WEB-INF/tlds/kentld.tld" prefix="tlt" %>
<%@page contentType="text/html" pageEncoding="UTF-8"%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">

<html>
<jsp:useBean id="org" class="businessLogic.Organization"/>
<%
String deptName = (String) request.getParameter("deptName");
businessLogic.Department dept = org.getDepartment(deptName);
%>
<head>
<meta http-equiv="Content-Type" content="text/html;
charset=UTF-8">
<title><%= deptName %></title>
</head>
<body>
<table>
<tr>
<th width="100">Name</th>
</tr>
<%-- List all department members --%>
<tlt:IterationTag name="member"
type="businessLogic.Member"
group="<%= dept.getMembers() %>">
<tr>
said:
</tr>
</tlt:IterationTag>
</table>
</body>
</html>

========= END EXAMPLE =========
 
A

Arne Vajhøj

Ken said:
I am trying to follow a sun tutorial for creating custom tags. (The
tutorial can be found here if needed: http://java.sun.com/products/jsp/tutorial/TagLibraries16.html
)

The issue:
The documentation here (http://java.sun.com/products/jsp/tags/11/
syntaxref11.fm10.html) states that I _must_ create or locate a bean
with <jsp:useBean> before I use <jsp:getProperty> on that bean. Now
in the Sun example the <jsp:getProperty> is not referencing the bean
from <jsp:useBean> but one from a custom tag.

If this is possible what requirements need to fulfilled so I can make
sure I am fulfilling them? I have done quite a bit of normal Java
programming but having just started with JSPs I'm finding the new
functionality magical.

I assume that the text is let us call it incomplete.

The bean need to be available in one of the scopes.

The only way of doing that in pure JSP is to use the useBean tag.

But you can also make it available from scriptlet Java code or from a
servlet or from some code called by a servlet.

BTW, with EL then I would consider the getProperty tag to be
obsolete.

Arne
 
A

Arne Vajhøj

Arne said:
I assume that the text is let us call it incomplete.

The bean need to be available in one of the scopes.

The only way of doing that in pure JSP is to use the useBean tag.

But you can also make it available from scriptlet Java code or from a
servlet or from some code called by a servlet.

BTW, with EL then I would consider the getProperty tag to be
obsolete.

Well not quite true. If you set the property with something other
than page scope then it apparantly has be with useBean !

(at least with my Tomcat version)

Example:

<%@ page import="test.C" %>
<%
request.setAttribute("v1a", new C("foo"));
session.setAttribute("v2a", new C("foo"));
application.setAttribute("v3a", new C("foo"));
pageContext.setAttribute("v4a", new C("foo"));
%>
<jsp:useBean id="v1b" scope="request" class="test.C"/>
<jsp:setProperty name="v1b" property="s" value="bar"/>
<jsp:useBean id="v2b" scope="session" class="test.C"/>
<jsp:setProperty name="v2b" property="s" value="bar"/>
<jsp:useBean id="v3b" scope="application" class="test.C"/>
<jsp:setProperty name="v3b" property="s" value="bar"/>
<jsp:useBean id="v4b" scope="page" class="test.C"/>
<jsp:setProperty name="v4b" property="s" value="bar"/>
${v1a.s}${v1b.s}<br/>
${v2a.s}${v2b.s}<br/>
${v3a.s}${v3b.s}<br/>
${v4a.s}${v4b.s}<br/>
foo<jsp:getProperty name="v1b" property="s"/><br/>
foo<jsp:getProperty name="v2b" property="s"/><br/>
foo<jsp:getProperty name="v3b" property="s"/><br/>
<jsp:getProperty name="v4a" property="s"/><jsp:getProperty name="v4b"
property="s"/><br/>

Arne
 
K

Ken

Hmm... My thoughts start more less complete but in the transcoding to
English sometimes words are lost. To make matters worse I only know
one language.
Well not quite true. If you set the property with something other
than page scope then it apparantly has be with useBean !

(at least with my Tomcat version)

Example:

<%@ page import="test.C" %>
<%
request.setAttribute("v1a", new C("foo"));
session.setAttribute("v2a", new C("foo"));
application.setAttribute("v3a", new C("foo"));
pageContext.setAttribute("v4a", new C("foo"));
%>
<jsp:useBean id="v1b" scope="request" class="test.C"/>
<jsp:setProperty name="v1b" property="s" value="bar"/>
<jsp:useBean id="v2b" scope="session" class="test.C"/>
<jsp:setProperty name="v2b" property="s" value="bar"/>
<jsp:useBean id="v3b" scope="application" class="test.C"/>
<jsp:setProperty name="v3b" property="s" value="bar"/>
<jsp:useBean id="v4b" scope="page" class="test.C"/>
<jsp:setProperty name="v4b" property="s" value="bar"/>
${v1a.s}${v1b.s}<br/>
${v2a.s}${v2b.s}<br/>
${v3a.s}${v3b.s}<br/>
${v4a.s}${v4b.s}<br/>
foo<jsp:getProperty name="v1b" property="s"/><br/>
foo<jsp:getProperty name="v2b" property="s"/><br/>
foo<jsp:getProperty name="v3b" property="s"/><br/>
<jsp:getProperty name="v4a" property="s"/><jsp:getProperty name="v4b"
property="s"/><br/>

Arne

Hmm... when I use the <jsp:getProperty>

The JSPs Java file has an error on the line:
out.write(org.apache.jasper.runtime.JspRuntimeLibrary.toString
(org.apache.jasper.runtime.JspRuntimeLibrary.handleGetProperty
(_jspx_page_context.findAttribute("member"), "email")));

It states:
string:///list_jsp.java:127: cannot find symbol
symbol : variable apache
location: class businessLogic.Organization

Why is this line being generated? Can I do something about the way
jsp:getProperty is translated? I am using GlassFish so I found it a
bit surprising to find reference to apache...

So I ask here but I guess I'll have to find the schema of the jsp tag
namespace and see if that helps...

The point isn't to create an iteration tag, I am just trying to
understand functional relationships between tags... But I didn't know
JSTL could address iteration, so I'll look into that after I
understand this issue. Because right now I have some really ugly JSPs/
Servlets need a beautiful stick and I think custom tags and probably
JSTL is the way to go.
 
A

Arne Vajhøj

Ken said:
Hmm... when I use the <jsp:getProperty>

The JSPs Java file has an error on the line:
out.write(org.apache.jasper.runtime.JspRuntimeLibrary.toString
(org.apache.jasper.runtime.JspRuntimeLibrary.handleGetProperty
(_jspx_page_context.findAttribute("member"), "email")));

It states:
string:///list_jsp.java:127: cannot find symbol
symbol : variable apache
location: class businessLogic.Organization

Why is this line being generated?

The lines tries to get a property member in a bean with id email.

I am wondering more about what the variable apache is!

What version of Tomcat are you using?
The point isn't to create an iteration tag, I am just trying to
understand functional relationships between tags... But I didn't know
JSTL could address iteration, so I'll look into that after I
understand this issue. Because right now I have some really ugly JSPs/
Servlets need a beautiful stick and I think custom tags and probably
JSTL is the way to go.

JSTL and EL is definitely the way to go.

Custom tags may be the way to go if you need something not covered
by JSTL.

Arne
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,770
Messages
2,569,583
Members
45,074
Latest member
StanleyFra

Latest Threads

Top