Drake Pyrosius wrote:
>
> 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.

It's not beautiful, but this should work:

  <%@ page import="Person, Address" %>

  <jsp:useBean id="person" class="Person" scope="request" />
  <%
    Address address = person.getAddress();
    pageContext.setAttribute("address", address);
  %>
  <jsp:getProperty name="address" property="city" />

Two things differ from your example:

1) I use a JSP scriptlet as opposed to a JSP declaration to retrieve
   the Address bean from the Person bean. JSP declarations end up
   outside the _jspService() method in the generated servlets, so
   variables declared in a declaration become instance variables
   instead of local variables.

2) I add the Address bean to the page scope, by calling setAttribute()
   on the pageContext object. The <jsp:getProperty> and <jsp:setProperty>
   actions (as well as custom actions) can only get access to objects
   available in one of the JSP scopes; they don't have access to
   scripting variables.

I also added a page directive to import the Person and Address classes,
since classes that do not belong to a package are not automcatically
visible in a class that does belong to a package (such as the generated
servlet class in most JSP containers).

Hans
--
Hans Bergsten           [EMAIL PROTECTED]
Gefion Software         http://www.gefionsoftware.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