File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Struts and the fly likes Struts Iterate tag, issue in populating form bean values Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Frameworks » Struts
Bookmark "Struts Iterate tag, issue in populating form bean values" Watch "Struts Iterate tag, issue in populating form bean values" New topic
Author

Struts Iterate tag, issue in populating form bean values

Narasimha Rao B.
Ranch Hand

Joined: Aug 26, 2002
Posts: 205
Hi,
I am using the iterate tag to populate the jsp page from formbean, it is working fine, but when the user modifies the values in the jsp page, when priting the values in the form bean it is printing the old values(intial values that are populated the jsp page) instead of the modified values in jsp page.

My JSP code looks like,
------------
<nested:iterate id="spreadDetails" name="limitSpreadActionForm" property="spreadList" type="com.code.ProfileComponent">
<tr><td>
<nested:text name="spreadDetails" property="value" />
</td> </tr>
</nested:iterate>
---------
In the JSP page, at the top formbean is defined as - 'limitSpreadActionForm', this formbean has a property named - 'spreadList', which is of type ArrayList, this ArrayList contains the Objects of type 'ProfileComponent', the 'ProfileComponent' class has a property name 'value'

When i run the code in debug mode, in BeanUtils.setProperty, method displays the values correctly(i.e. Modified values in the JSP page), but when i try to print the values in the ActionForm class, only the old values are printing, not the user modified values.


Thanks in Advance.


Narasimha
Merrill Higginson
Ranch Hand

Joined: Feb 15, 2005
Posts: 4864
You need to use indexed properties for this. The nested:text tag does the JSP part automatically, but you still need to create an indexed getter in your ActionForm. Specifically, you'll need a method named getSpreadDetails that takes an integer as a parameter and returns a single ProfileComponent for that index in the list. If your ActionForm is in request scope, you will need to give the method lazy initialization behavior. All of this is explained in much greater detail in question 6 of the JavaRanch Struts FAQ


Merrill
Consultant, Sima Solutions
Narasimha Rao B.
Ranch Hand

Joined: Aug 26, 2002
Posts: 205
Hi Merrill,

Thanks so much for your inputs, i have seen your many postings and got the good information, still i have some issues, it would be great if you can help.
My issue is, i can display the values in the jsp page, but when i try to submit the page, modified values are not populating to the form bean, instead old values are populating.

My code details are -

In my Action class, i have a ArrayList, which holds list of 'Profile', the 'Profile' Class has one property name 'Rating' and a collection of 'ProfileComponent' and 'ProfileComponent' has a property named 'Value'. My requirement is loop through top ArrayList and display one row each for one ArrayList element, and in each row display first 'Rating' and then loop through collection of 'ProfileComponent' and display the property 'Value'. The display part is fine, but when the user modifies the 'Rating' or 'Value' (of 'ProfileComponent'), the modified 'Rating' values are reflecting in ActionForm bean, but the modified 'Value' are not reflecting in ActionForm bean.

JSP Code:
------
<nested:iterate name="spreadActionForm" id="spreadItem" property="spreadList" >
<tr>
<td>
<html:text name="spreadItem" property="rating" indexed="true" />
</td>
<nested:iterate name="spreadItem" property="profileCompoList" id="profileComp">
<td>
<nested:text name="profileComp" property="value" indexed="true" />
</td>

</nested:iterate>

</tr>
</nested:iterate>

-------------------------------------------
ActionForm :
--------------

public void setSpreadList(List spreadsList){ this.spreadList = spreadsList;}
public List getSpreadList(){ return this.spreadList;}
private List spreadList;

public Profile getSpreadItem(int index){
if(this.spreadList == null){ this.spreadList = new ArrayList();}
return (Profile) spreadList.get(index);}



Profile class:
--------------
public ArrayList getProfileCompoList(){ return this.profileCompoList; }
public void setProfileCompoList(ArrayList profiles){ this.profileCompoList = profiles ; }
private ArrayList profileCompoList;

public String getRating() { return rating; }
public void setRating(String rating) { this.rating = rating; }
private String rating;

public ProfileComponent getProfileComp(int index){return ((ProfileComponent)profileCompoList.get(index));}

ProfileComponet class:
-------------------
public double getValue() { return value; }
public void setValue(double value) {this.value = value; }
private double value;
-----------------------------


Thanks so much in Advance.
Narasimha Rao B.
Ranch Hand

Joined: Aug 26, 2002
Posts: 205
Any help please.

Thanks.
Brent Sterling
Ranch Hand

Joined: Feb 08, 2006
Posts: 948
I have never used the nested tags. I looked at your code and nothing really jumped out at me. I take it that you have your action configured to use session scope. I would try to set breakpoints in all your get and set methods to figure out what is being called. I have used indexed properties in about 20 pages and only 1 or 2 of them worked the first time. Probably only half of them were working on the 10th try.

- Brent
Merrill Higginson
Ranch Hand

Joined: Feb 15, 2005
Posts: 4864
When using indexed properties and going more than one level deep, I have found that it's best not to use nested tags or indexed="true" but to manually construct the name of the property to be sure that the indexing is correct. Here's how I'd do it:


If your web application is a servlet Version 2.4 application, you can use EL expressions as I've done above. Otherwise, you have to either use the EL version of the Struts tags or use a scriptlet instead of EL expressions.
[ September 16, 2007: Message edited by: Merrill Higginson ]
 
wood burning stoves
 
subject: Struts Iterate tag, issue in populating form bean values