aspose file tools*
The moose likes Struts and the fly likes Help with Indexing problem using logic:iterate and nested:iterate Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Frameworks » Struts
Bookmark "Help with Indexing problem using logic:iterate and nested:iterate" Watch "Help with Indexing problem using logic:iterate and nested:iterate" New topic
Author

Help with Indexing problem using logic:iterate and nested:iterate

Adrian Marrero
Greenhorn

Joined: Oct 23, 2007
Posts: 10
Code:
<logic:iterate id="modules" name="dbItnForm" property="itnModules" indexId="moduleIndex">
<bean:write name="modules" property="displayName"/>
<nested:iterate name="modules" property="properties" indexId="propIndex">
<nested:write property="displayName"/>
<nested:text property="value" indexed="true"/>
</nested:iterate>
</logic:iterate>

When the html output shows:
Item 1) modules[0].properties[0].value
Item 2) modules[0].properties[0].value
modules[1].properties[1].value
modules[2].properties[2].value
Item 3) modules[0].properties[0].value
modules[1].properties[1].value
...

The index of properties is being applied to modules. What I need is.......

Item 1) modules[0].properties[0].value
Item 2) modules[1].properties[0].value
modules[1].properties[1].value
modules[1].properties[2].value
Item 3) modules[2].properties[0].value
modules[2].properties[1].value

Can anyone offer any suggestions? Thank you ahead of time for your help
Merrill Higginson
Ranch Hand

Joined: Feb 15, 2005
Posts: 4864
When using indexed properties that go more than one level deep I've found that it's better to manually build the indexed property name using scriptlets or EL expressions rather than trusting the nested:xxx tags or the indexed="true" attribute. These just don't always work the way you want them to. Here's an example:

The above assumes that you either have a Servlet Version 2.4 application, or that you are using the struts-el version of the tags. You could accomplish the same thing with scriptlets.


Merrill
Consultant, Sima Solutions
Adrian Marrero
Greenhorn

Joined: Oct 23, 2007
Posts: 10
Thank you very much for your suggestion. However I tried it with a scriptlet and received and error. Here is the code that I tried...

<html:form action="/job/submitItn" method="post">
<logic:iterate id="modules" name="dbItnForm" property="itnModules" indexId="moduleIndex">
<bean:write name="modules" property="displayName"/>
<nested:iterate name="modules" property="properties" indexId="propIndex">
<nested:write property="displayName"/>
<nested:text property='<%= "modules[" + moduleIndex + "].properties[" + propIndex + "].value"%>'/>
</nested:iterate>
</logic:iterate>
<html:submit>Submit Job</html:submit><html:reset value="Reset Job Parameters"/>

and the error that I get is the following...

[ServletException in:/customizeItn.jsp] No getter method for property properties[0].modules[0].properties[0].value of bean modules'

Is it correct?

I also have another question...As it is right now the values of the fields are not getting passed when the form is submitted. How can I pass the values to the servlet without passing hidden fields? It seems as if the set method is never getting called when I submit. Also, if this is achieved would I not have to worry about the indexing problem? Would the code use the correct set methods in the nested beans?
Merrill Higginson
Ranch Hand

Joined: Feb 15, 2005
Posts: 4864
First of all, when manually building indexed property names, never use <nested:text>, but <html:text> instead. The nested tags manipulate the property name, and the whole point of manually building the property name is to do it right yourself and avoid "under the covers" manipulation that is not working right.

In answer to your second question: If you get the indexed property name right and have created indexed getters in your form beans, then the values will be propagated to the form correctly. For more information and examples on indexed properties, see question 6 of this forum's FAQ.
Adrian Marrero
Greenhorn

Joined: Oct 23, 2007
Posts: 10
Merill,

Thank you very much for your suggestions and replies to my questions. I was able to successfully index and set the different properties in my nested structure.

Here is what I did.

The three levels are as follows...
DatabaseItn
MBean
PBean
In DatabaseItn I needed to implement the ListUtils.lazylist feature in the reset method.
In MBean I implemented the indexed get method for the properties field.
And in PBean the fields were accessed correctly.

As for my jsp code I referenced the DatabaseItn form in the name attribute of the html/nested:text tag while using the scriptlet in the property attribute to correctly reference the index and field that I wanted. Here is how my jsp code looks.

<logic:iterate id="modules" name="dbItnForm" property="itnModules" indexId="moduleIndex">
<bean:write name="modules" property="displayName"/>
<nested:iterate id="moduleProps" name="modules" property="properties" indexId="propIndex">
<nested:write property="displayName"/>
<nested:text name="dbItnForm" property='<%="itnModules["+ moduleIndex +"].properties["+ propIndex +"].value"%>'/>
</nested:iterate>
</logic:iterate>

Once again, thank you for your suggestions and if you or anyone feels that I can structure this more properly please let me know. Thank you and have a nice day.
webber
Greenhorn

Joined: Nov 02, 2007
Posts: 1
I have tried the above suggestion .. here is my code .. but it throws the following error .. Please help!


<TABLE align="center">
<logic:iterate id="question" name="page" property="questions" type="Question" indexId="index1" >
<tr>
<th align="center"><bean:write name="question" property="questionId"/>. <bean:write name="question" property="text"/></th>
</tr>
<nested:iterate id="answer" name="question" property="answers" type="Answer">
<tr>
<td>
<nested:equal value="1" name="question" property="type">
<nested:radio property="<%= "org.apache.struts.taglib.html.BEAN["+ index1 + "].response" %>" value="<%=(question.getQuestionId() + "=" + answer.getAnswerId())%>"><nested:write property="text"/></nested:radio>
</nested:equal>
<nested:equal value="2" name="question" property="type">
<nested:checkbox property="<%= "org.apache.struts.taglib.html.BEAN["+ index1 + "].response" %>" value="<%=(question.getQuestionId() + "=" + answer.getAnswerId())%>"><nested:write property="text"/></nested:checkbox>
</nested:equal>
</td>
</tr>
</nested:iterate>
</logic:iterate>
</TABLE>


Error:
----------
java.lang.IllegalArgumentException: Property 'answers' is not indexed
org.apache.commons.beanutils.PropertyUtilsBean.getIndexedProperty(PropertyUtilsBean.java:429)
org.apache.commons.beanutils.PropertyUtilsBean.getIndexedProperty(PropertyUtilsBean.java:340)
org.apache.commons.beanutils.PropertyUtilsBean.getNestedProperty(PropertyUtilsBean.java:664)
org.apache.commons.beanutils.BeanUtilsBean.getNestedProperty(BeanUtilsBean.java:698)
org.apache.commons.beanutils.BeanUtilsBean.getProperty(BeanUtilsBean.java:723)
[ November 02, 2007: Message edited by: rancher ]
Merrill Higginson
Ranch Hand

Joined: Feb 15, 2005
Posts: 4864
rancher,

Please check your private messages for an important message from me. You can do so by clicking My Private Messages in the upper right corner of the page.
Cendy Van
Greenhorn

Joined: Nov 06, 2007
Posts: 6
I suppose "answers" is an implementation of java.util.Set Interface.
There is no indexed getter for this kind of object.
Try using an implementation of the java.util.List interface.
Merrill Higginson
Ranch Hand

Joined: Feb 15, 2005
Posts: 4864
Webber,

Thanks for taking the time to change your display name. However, you're not quite there yet. The JavaRanch Naming Policy requires that you display both a first and last name. If you don't want to display your real name, that's fine, but the name you display must at least appear to be a real name.
 
 
subject: Help with Indexing problem using logic:iterate and nested:iterate