Hello all,

Does anyone have any recommendations on handling nested JavaBeans in a JSP?
For example:

public class Address
{
private String firstLine, secondLine;
private String city, state, zip;

set/get() ...
}

public class Person
{
private String name;

getName/setName() ...
}

And in the JSP:

<jsp:useBean id="person" class="Person" scope=... />

The tag <jsp:getProperty> will only return Strings and as such getting to
the nested Address object's individual properties is difficult.

Attempting the following on Tomcat fails:

<jsp:useBean id="person" class="Person" scope="request" />
<%! Address address = person.getAddress(); %>
<jsp:useBean id="address" class="Address" scope="request" />

Apparently in the generated source another instance of Address is
initialized which hides the one that is manually declared.

Another attempted workaround also fails:
<jsp:useBean id="person" class="Person" scope="request" />
<jsp:useBean id="address" class="Address" scope="request">
<% address = person.getAddress(); %>
</jsp:useBean>

Similar problems crop up in the generated source as the first hack.

I seem to recall a NESTED modifier to the bean tag in 0.92 though I don't
recall it's use and can find no mention of it in 1.1.

I see two alternatives, but I hope someone can suggest better ways:

1) Having the forwarding servlet (model 2) place the nested bean in the
Request object along with the enclosing bean. e.g.
req.setAttribute("person", personObj);
req.setAttribute("address", personObj.getAddress());
This doesn't make much sense considering person contains an
address. They don't belong on the same level structure-wise and just from an
intuitive standpoint one should be able to navigate down the tree of
JavaBean properties.

2) Create a custom tag along the lines of
<taglib:useNestedBean ... />
This seems an extraordinary and non-standard thing to do. It's bad enough
that iteration tags have to be implemented in a proprietary fashion.

In any case, if anyone has any suggestions I would be appreciative.

Thanks,
Anthony Yu


-----------------------------------------------
FREE! The World's Best Email Address @email.com
Reserve your name now at http://www.email.com

===========================================================================
To unsubscribe: mailto [EMAIL PROTECTED] with body: "signoff JSP-INTEREST".
Some relevant FAQs on JSP/Servlets can be found at:

 http://java.sun.com/products/jsp/faq.html
 http://www.esperanto.org.nz/jsp/jspfaq.html
 http://www.jguru.com/jguru/faq/faqpage.jsp?name=JSP
 http://www.jguru.com/jguru/faq/faqpage.jsp?name=Servlets

Reply via email to