[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Struts: form with multiple text fields with same name.



Poorav,

Struts doesn't work they way you expect it to.  When
you give all the fields the same name, struts has no
way to know which text field should display which
String from the array.

Here's how to work around it
----------------------------

When you use the struts-html tags, give each property
a subscript.  For example
  <html:text property="myField[0]" name="myForm"/>
  <html:text property="myField[1]" name="myForm"/>
  <html:text property="myField[2]" name="myForm"/>

In your form class, implement get and set methods that
also take an index.  For example
  public void setMyField( int index, String value )
  {
    ...
  }
  public String getMyField( int index )
  {
    ...
  }

The biggest benefit to using this struts feature, is
that it lets you work with a dynamic number of fields
on a page.  You can certainly put the <html:text> tag
inside a loop to create many fields in the output.


Kevin Chipalowsky



--- Poorav Chaudhari <pooravc@yahoo.com> wrote:
> In my web form, i have many similar fields. I have
> named all of them the same.
> for example <html:text property="referenceName"
> name="someForm"/> and there are
> five of these. my actionform bean has an array
> declared for these fields and so
> hopefully the five fields should populate the array.
> and in fact it does. but
> my problem is that if my form is invalidated, and my
> action class takes me back
> to the same page the all the text fields show some
> funny junk something like 
> [Ljava.lang.String;@55a 
> instead of the original value that i had entered.
> (if there was no error, and i
> access the field through the form i get the correct
> value). 
> which is probobaly the memory address for that array
> cell, i don't know. why is
> this happening? is there any documentation out there
> that shows how to deal
> with this type of form? in my jsp form i have
> explicitely typed out these five
> fields and not used any iteration function to
> display it. does this have
> something to do with this. 
> 
> your help will be much appreciated.
> 
> Thanks
> 
> =====
> Poorav Chaudhari
> 
> __________________________________________________
> Do you Yahoo!?
> Yahoo! Web Hosting - establish your business online
> http://webhosting.yahoo.com
> 


__________________________________________________
Do you Yahoo!?
Yahoo! Web Hosting - establish your business online
http://webhosting.yahoo.com