Yeah, I knew that it wouldn't be able to convert to objects, that's why
I had the setter:
public void setFlexfields(int index, String iNewVal) {
_flexFields[index].setValue(iNewVal);}
It wasn't clear in the documentation how this would work with arrays, but in the bean
tag documentation, it does talk about setting properties like setFlexfields(2,
"value"), which is why I wrote the method.
The message below did help me. For those who are interested, and for those who search
for this problem, here's what I had to do. I added a new setter:
public void setTempflexfields(String[] newValues)
{
for (int i = 0; i < newValues.length; i++)
{
_flexFields[i].setValue(newValues[i]);
}
}
In my jsp, I changed it so that it used "tempflexfields" as the name of the hiddens
and text boxes. Since I specify the value explicitly, I don't need to worry about
having a getter getTempflexfields.
When the form is submitted, it sends the string array into setTempflexfields. I need
to then go through and really assign the values to the inner array of objects.
This method works, but there are a couple caveats.
1. You MUST write out the hidden fields if some elements of your array are
non-editable. Otherwise, when you submit, you indexes into the arrays will be off.
2. You'll have a mixture of <input type="text"> and <input type="hidden"> in the end
html. I don't know that there is any guarantee that the order of the values sent back
will match to the order of the array. In otherwords, I want "value1" to go into
element 1, but maybe "value1" will be sent as the 5th element of the array?
Everything seems to work fine for me, I just not sure that the order is guaranteed.
Hopefully somebody will find this useful.
-- dave
Elankath, Tarun (Cognizant) wrote:
>Please correct me if I am wrong but I think all the setters in your form
>beans need to take Strings as parameters.
>
>I don't think Struts provides a way as yet to convert Strings into objects.
>I have got to check whether 1.1 addresses this issue.
>
>Everybody in Struts (except guys who did cgi programming) trips over this. I
>wonder why can't they they explicitly mention this in the ActionForm
>documentation.
>
>Tarun
>
>
>
>
>
>-----Original Message-----
>From: David Graham [mailto:[EMAIL PROTECTED]]
>Sent: Thursday, July 25, 2002 8:52 PM
>To: [EMAIL PROTECTED]
>Subject: Re: Help with Arrays of text fields
>
>
>I've done something similar with hidden fields but have not tried to
>repopulate them. I had several hidden fields named "required". My
>ActionForm looked like this:
>
>private String[] required;
>public String[] getRequired(){return required;}
>public void setRequired(String[] required){this.required=required;}
>
>Struts successfully filled the string array with the values from each hidden
>
>field.
>
>Hope this helps.
>
>
>
>>From: David Wood <[EMAIL PROTECTED]>
>>Reply-To: "Struts Users Mailing List" <[EMAIL PROTECTED]>
>>To: Struts Users Mailing List <[EMAIL PROTECTED]>
>>Subject: Help with Arrays of text fields
>>Date: Thu, 25 Jul 2002 11:02:15 -0400
>>
>>I have a form that has a variable number of text boxes on it. I can
>>populate the form correctly, but on submit, struts is unable to put the
>>values back into the ActionForm. To complicate things just a little, these
>>
>>
>
>
>
>>text boxes are defined as other classes, since they store a label, a
>>default value, and an enabled flag.
>>
>>So, I have an object called FlexField. Flex field has the following
>>methods:
>> public String getName() { return _name;}
>> public String getValue() { return _value;}
>> public void setValue(String iNewValue) { _value = iNewValue;}
>> public boolean getEnabled() { return _enabled;}
>>
>>My ActionForm has an array of FlexFields and methods for setting the array
>>itself and individual values into the array.
>> public FlexField[] getFlexfields() { return _flexFields;}
>> public FlexField getFlexfields(int index) { return _flexFields[index];}
>> public void setFlexfields(FlexField[] iNewVal) { _flexFields = iNewVal;}
>> public void setFlexfields(int index, String iNewVal) {
>>_flexFields[index].setValue(iNewVal);}
>>
>>My Action creates an array of flex fields and sets it into the ActionForm
>>before redirecting to the jsp that shows these.
>>
>>My jsp puts out the list of flex fields into a table using the
>>logic:iterate tag:
>><logic:iterate name="myForm" id="flexfield" property="flexfields"
>>type="com.temp.FlexField" >
>> <tr>
>> <td align="right"><bean:write name="flexfield" property="name"/></td>
>> <td> </td>
>> <td align="left">
>> <logic:equal name="flexfield" property="enabled" value="true">
>> <html:text name="myForm" property="flexfields"
>>value="<%=flexfield.getValue()%>"/>
>> </logic:equal>
>> <logic:equal name="flexfield" property="enabled" value="false">
>> <hidden name="conferenceForm" property="flexfields"
>>value="<%=flexfield.getValue()%>"/>
>> <bean:write name="flexfield" property="value"/>
>> </logic:equal>
>> </td>
>> </tr>
>></logic:iterate>
>>
>>
>>What I end up with is a bunch of text boxes and hidden fields that all have
>>
>>
>
>
>
>>the name "flexfields", which is the name of the property in my ActionForm.
>>But when I submit the form, I get an exception saying that it can't
>>populate the ActionForm. If you have multiple text boxes with the same
>>name on a form, how does struts try to populate the ActionForm? Does it
>>just make X number of calls to setFlexfield(String input) without regard to
>>
>>
>
>
>
>>indexing the elements?
>>
>>I know this is a long and complicated example, but any help would be
>>appreciated.
>>
>>-- dave
>>
>>
>>--
>>To unsubscribe, e-mail:
>><mailto:[EMAIL PROTECTED]>
>>For additional commands, e-mail:
>><mailto:[EMAIL PROTECTED]>
>>
>>
>
>
>
>
>_________________________________________________________________
>MSN Photos is the easiest way to share and print your photos:
>http://photos.msn.com/support/worldwide.aspx
>
>
>--
>To unsubscribe, e-mail:
><mailto:[EMAIL PROTECTED]>
>For additional commands, e-mail:
><mailto:[EMAIL PROTECTED]>
>
>
>------------------------------------------------------------------------
>
>
>This e-mail and any files transmitted with it are for the sole use of the intended
>recipient(s) and may contain confidential and privileged information.
>If you are not the intended recipient, please contact the sender by reply e-mail and
>destroy all copies of the original message.
>Any unauthorised review, use, disclosure, dissemination, forwarding, printing or
>copying of this email or any action taken in reliance on this e-mail is strictly
>prohibited and may be unlawful.
>
> Visit us at http://www.cognizant.com
>
>
>
>
>------------------------------------------------------------------------
>
>--
>To unsubscribe, e-mail: <mailto:[EMAIL PROTECTED]>
>For additional commands, e-mail: <mailto:[EMAIL PROTECTED]>
>