In a Struts form, able to read values from an array using <logic:iterate> but not update them.
4 posts in topic
Flat View  Flat View
TOPIC ACTIONS:
 

Posted By:   Michael_Link
Posted On:   Friday, November 8, 2002 06:36 AM

I have a JavaBean in my session called Event. This bean contains an array of Param objects and has get and set methods as below to retrieve the array. I can edit all other "simple" values in the bean, but not the array. public class Event { ... private Param[] paramlist; ... public Param[] getParamlist() { return paramlist; } public void setParamlist(Param[] paramlist) { this.paramlist = paramlist; } } The Jsp form simply extracts these parameter objects and uses the element to display them. type="Param" name=" <%= Config.EVENT_ATTRIBUTE %   More>>

I have a JavaBean in my session called Event. This bean contains an array of Param objects and has get and set methods as below to retrieve the array. I can edit all other "simple" values in the bean, but not the array.


			
public class Event {
...
private Param[] paramlist;
...

public Param[] getParamlist() {
return paramlist;
}

public void setParamlist(Param[] paramlist) {
this.paramlist = paramlist;
}
}


The Jsp form simply extracts these parameter objects and uses the element to display them.


			

type="Param"
name=" <%= Config.EVENT_ATTRIBUTE %>"
property="paramlist"
scope="session">

Name:

Value:






I can change the values (i.e. the second of the 2 html:text fields) on the web page, but when the form is submitted the changes are not reflected in the Event bean.


Is there anybody who can offer one or two lines of wisdom? Help very much appreciated!


Thanks, Mike

   <<Less

Re: In a Struts form, able to read values from an array using &lt;logic:iterate&gt; but not update them.

Posted By:   Ted_Husted  
Posted On:   Saturday, November 9, 2002 08:30 PM


Another way to go is to put name and value into separate arrays. You can then follow the pattern described here:



Struts Tip #6



You would then also add a setter and getter to the ActionForm that would transform the paramist into the dual arrays and back again.



This method also works well with the multibox tag. But if that's not a consideration, then Amiga's solution looks quite good. I believe Thomas Hock's solution requires use of the Struts 1.1 beta or a nightly build (to get the indexed=true functionality).



HTH, Ted.

Re: In a Struts form, able to read values from an array using &lt;logic:iterate&gt; but not update them.

Posted By:   Thomas_Hock  
Posted On:   Saturday, November 9, 2002 03:09 AM

Sorry I forgot something:

add the attribute indexed="true" to your html:text tags.

Re: In a Struts form, able to read values from an array using &lt;logic:iterate&gt; but not update them.

Posted By:   Thomas_Hock  
Posted On:   Saturday, November 9, 2002 12:46 AM

Add the following method to your JavaBean:

public Param getParams(int i) {
return this.paramlist[i];
}

This will work.

In common:
1. Your JavaBean needs to have a getter method which returns element i of your collection.
2. The method's name must match with the name of the id attribute of the logic:iterate tag.

Have fun THomas

Re: In a Struts form, able to read values from an array using &lt;logic:iterate&gt; but not update them.

Posted By:   Matthew_Osentoski  
Posted On:   Friday, November 8, 2002 10:44 AM

Hi Mike,

This is a good question. I'm still green when it comes to struts but, here is the problem I see. If you look at the HTML source produced by this JSP, you will get something like:



[td]Name:[/td]

[td][input type="text" name="name" ...][/td]

[td]Value:[/td]

[td][input type="text" name="value" ...][/td]

[td]Name:[/td]

[td][input type="text" name="name" ...][/td]

[td]Value:[/td]

[td][input type="text" name="value" ...][/td]

[td]Name:[/td]

[td][input type="text" name="name" ...][/td]

[td]Value:[/td]

[td][input type="text" name="value" ...][/td]

etc, etc..



The point is how does your action know which 'name' or 'value' should be associated with the correct place in your paramList array?



The final name/value pair returned will only be a single set of values. It seems to me that a tag similar to multibox would need to exist.



So, I also share your question. Can this functionality be done with the existing tagset or would one need to write a custom tag that can keep track of an index?
About | Sitemap | Contact