permaculture playing cards*
The moose likes Struts and the fly likes Struts2 nested iterators with listboxes Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Frameworks » Struts
Bookmark "Struts2 nested iterators with listboxes" Watch "Struts2 nested iterators with listboxes" New topic
Author

Struts2 nested iterators with listboxes

Casey Cox
Greenhorn

Joined: Nov 28, 2008
Posts: 14
I am stumped with this problem that I have. Any pointers to the solution or related documentation would be greatly appreciated.

My JSP has nested iterators, and the deepest one has a listbox, like so:


The generated source code looks like this:



And it seems to look alright to me. But submit causes a problem because it is not able to find a setter. I am not exactly sure where or how I would write this setter. I have a setter/getter for ProductBOs in the action class. The ProductBO class has a setter for the attributeBOs. What else do I need?

Thanks !

[ December 21, 2008: Message edited by: Casey Cox ]
[ December 21, 2008: Message edited by: Casey Cox ]
Casey Cox
Greenhorn

Joined: Nov 28, 2008
Posts: 14
Well, it seems like I had to declare my object as an Arraylist in the Action, like so:
ArrayList productBOs;
instead of
ProductBO[] productBOs;

So, now I am able to submit the jsp without running into an exception, but I don't get the values in my action.

I can see the setter being called, but the argument is always non null and empty!!


This is how the jsp looks like:


Help, anyone!
[ December 25, 2008: Message edited by: Casey Cox ]
Casey Cox
Greenhorn

Joined: Nov 28, 2008
Posts: 14
I think I found a way to get my stuff to work. I made my action implement ParameterAware and the jsp values are now available in the map. I am not sure if this is the correct way to do it, but it does seem to fulfil my needs, in a somewhat roundabout way.

I have been working on this for almost 2 days, and even though I have a possible solution in hand, I don't feel any relief, only a vague sense of anti-climax..
Nitin Surana
Ranch Hand

Joined: Jan 21, 2011
Posts: 129

@Cox Help me dude....I got stuck with the same issue....please help....its urgent....
How did you used parameteraware interface...can you post the code please.
Nitin Surana
Ranch Hand

Joined: Jan 21, 2011
Posts: 129

I Got the Solution:

This is how the JSP code will look like:

<s:form action="saveaction" >
<s:iterator value="lstBean" id="lstBean" status="outerStat">
<s:textfield value="%{name}" name="lstBean[%{#outerStat.index}].name"/>
<s:textfield value="%{amt}" name="lstBean[%{#outerStat.index}].amt"/>
<s:textfield value="%{id}" name="lstBean[%{#outerStat.index}].id"/>
<s:iterator value="%{lstString}" status="myStat">
<s:textfield name="lstBean[%{#outerStat.index}].lstString[%{#myStat.index}]"/>
</s:iterator>
</s:iterator>
<s:submit value="Click me to submit lstBean"/>
</s:form>


Following is the bean(XBean) whose List is used in the JSP:

public class XBean
{
private ArrayList<String> lstString=new ArrayList<String>();
private String name;
private Double amt;
private Integer id;
//Getters and setters of fields
}

Now you can simply have a field lstBean with setters in your submitting action (saveaction) and hey you are done.
Nitin Surana
Ranch Hand

Joined: Jan 21, 2011
Posts: 129

This is how the JSP code will look like:



Following is the bean(XBean) whose List is used in the JSP:


Now you can simply have a field lstBean with setters in your submitting action (saveaction) and hey you are done.
Vallaru smitha
Ranch Hand

Joined: Aug 19, 2008
Posts: 87

Hi Nitin,

i have followed your code but still i am not getting value, here is what i wrote

<s:iterator value="activityList" id="myact" status="activityStatus">

<s:iterator value="%{favorites}" status="myStat">
<s:checkbox name="myact[%{#activityStatus.index}].favorites[%{#myStat.index}].accFavorities"/>
</s:iterator>

But check box is not getting displayed.

Is there any other way it could be done. I am using one-to=many relationship between activity and favorites.

thanks
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Struts2 nested iterators with listboxes
 
Similar Threads
struts2:Displaying dtatbase values in jsp
dynamic checkboxes in struts 2
Struts 2 iterate over an arraylist of T.O.
validate in the s:iterator
how to start an action from a link...