File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Struts and the fly likes Displaying nested objects - e.g. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Frameworks » Struts
Bookmark "Displaying nested objects - e.g." Watch "Displaying nested objects - e.g." New topic

Displaying nested objects - e.g.

Nina Anderson
Ranch Hand

Joined: Jul 18, 2006
Posts: 148

I'm new to struts and I'm trying to write a value by de-referencing a bean hierarchy. This is my bean structure:

PackageDTO {

PackageCbo packageCbo
List packageItems

PackageCbo {

String packageName
String packagePrice

I'm passing a list of PackageDTOs in my jsp and I'm trying to access the packageName and packagePrice. I not idea how to achieve this. PLeAsE HeLP!!

Here's the code I have so far and nothing is displaying on my page:

####### MY JSP:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<%@ taglib uri="/WEB-INF/struts-html.tld" prefix="html"%>
<%@ taglib uri="/WEB-INF/struts-bean.tld" prefix="bean"%>
<%@ taglib uri="/WEB-INF/struts-logic.tld" prefix="logic"%>
<%@ taglib uri="/WEB-INF/struts-nested.tld" prefix="nested"%>
...some header settings

<nested:iterate name="PackageForm" id="dto" property="packageDTOList" >
<nested:radio property="packageCbo.packageName" />
<nested:write property="packageCbo.packagePrice" />


<form-bean name="PackageForm" type="org.apache.struts.action.DynaActionForm">
<!-- Dynamic properties of the Package Form -->
<form-property name="packageDTOList" type="java.util.Collection" />
<form-property name="packageDTO" type="com.PBCoreApp.datatransfer.PackageDTO" />
<form-property name="packageCbo" type="com.PBCoreApp.datamodel.PackageCbo" />
<form-property name="packageFeature" type="java.lang.String" />
<form-property name="selectedPackage" type="java.lang.String" />
<form-property name="packageName" type="java.lang.String" />

I would appreciate your assistance!
Merrill Higginson
Ranch Hand

Joined: Feb 15, 2005
Posts: 4864
For this, you will need to use "indexed properties". It takes a bit of effort to learn how to use them. The best place to start would be this link in the JavaRanch Struts FAQ Pay special attention to the example application that is shown there.

Consultant, Sima Solutions
Nina Anderson
Ranch Hand

Joined: Jul 18, 2006
Posts: 148
Thanks for the response...

It seems like the index property is being used to access objects in a list or collection. This is not what I need. The id field in nested:iterate or logic:iterate already grabs the object.

The problem I'm having is to traverse down the objects to access the values I want. Here's the hierarchy:

-> packageDTO
-> packageCbo
-> packageName

I'm get a NULLPointerException when I changed my code to this:

<logic:iterate name="PackageForm" id="packageDto" property="packageDTOList" >
<bean:write name="packageDto" property="packageCbo.packageName" />

#### Error ######

[3/18/07 11:17:14:188 PST] 00000049 WebApp E SRVE0026E: [Servlet Error]-[/web/services/servicePackage.jsp]: java.lang.NullPointerException
at org.apache.commons.beanutils.PropertyUtils.getSimpleProperty(
at org.apache.commons.beanutils.PropertyUtils.getNestedProperty(
at org.apache.commons.beanutils.PropertyUtils.getProperty(
at org.apache.struts.util.RequestUtils.lookup(
at org.apache.struts.taglib.logic.IterateTag.doStartTag(

I'm totally lost. PleAse Help!!!
Merrill Higginson
Ranch Hand

Joined: Feb 15, 2005
Posts: 4864
If all you want to do is display information using <bean:write> tags, then you are right: you don't need indexed properties for that. However, if you want to input values from a list of objects then you do need to use indexed properties. Since your first post contained an example of an input control using a property in a list, that's why I suggested indexed properties.

Regarding your second example: The code in the JSP looks correct. I suspect the problem is that your objects are not properly populated prior to the JSP being displayed. If the getPackageCBO method on any of the PackageDTO objects in the list returns null, that would cause this problem.
[ March 18, 2007: Message edited by: Merrill Higginson ]
I agree. Here's the link:
subject: Displaying nested objects - e.g.
It's not a secret anymore!