Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Arrays in struts ActionForm showing '[Ljava.lang.String;@10321032'

 
John Zwick
Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In my page, if the user submits a form and there are errors it will go back to the form input page with error messages..

However, when we go back to the page, the text input boxes are not showing what the user inputted... instead, something like "[Ljava.lang.String;@10321032" is shown

My the form bean that corresponds to that text input box is a string array
This is my form bean:



Where am i going wrong here?
 
Ankit Garg
Sheriff
Posts: 9529
33
Android Google Web Toolkit Hibernate IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There is only one stockId text field in your JSP page. Then why are you using an array in the Action Form?? If you really want an array, then you should probably iterate over that array in your JSP and display multiple stockId text fields...
 
John Zwick
Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ankit Garg wrote:There is only one stockId text field in your JSP page. Then why are you using an array in the Action Form?? If you really want an array, then you should probably iterate over that array in your JSP and display multiple stockId text fields...


I have one row intially but the user can dynamically add rows of inputs via Javascript so theres a possibility of 1 to many text fields
 
Himanshu Kansal
Ranch Hand
Posts: 257
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello,

According to what you are trying to do, things are happening correctly. You asked it to output the array object and that is what it gave u.

Concept of arrays for forms is that when you have a control like a series of checkboxes, you can use an array in your form to hold multiple selected values. The values in case of textboxes would normally be overwritten, instead of being mapped to arrays. I am not very sure but I think this is what happens to textboxes.

Still, if I assume that in case you have multiple textboxes, all with the same name attribute, and they do get mapped to the array in your form, then when you have a single textbox, you still would require to refer to its value through the array index, stockID[0] that is.
 
John Zwick
Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Himanshu Kansal wrote:Hello,

According to what you are trying to do, things are happening correctly. You asked it to output the array object and that is what it gave u.

Concept of arrays for forms is that when you have a control like a series of checkboxes, you can use an array in your form to hold multiple selected values. The values in case of textboxes would normally be overwritten, instead of being mapped to arrays. I am not very sure but I think this is what happens to textboxes.

Still, if I assume that in case you have multiple textboxes, all with the same name attribute, and they do get mapped to the array in your form, then when you have a single textbox, you still would require to refer to its value through the array index, stockID[0] that is.


why cant a single text box map to an array of size 1?
If I refer to its value through the array index, when i add a new row of inputs (which clones the first row of inputs), we will have two input boxes which refer to the same array index.

For example:
Before adding a new row we will have:
"<input type="text" name="stockID[0]" maxlength="10" size="10" "

but after adding another row of inputs, we will get:
"<input type="text" name="stockID[0]" maxlength="10" size="10"
"<input type="text" name="stockID[0]" maxlength="10" size="10"

and when the form gets saved, only one of the inputs is stored and not both.
 
Himanshu Kansal
Ranch Hand
Posts: 257
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Writing stockID[0] does no good. While mapping the setter from the bean is called. Here it would try to call setStockID[0]() which obviously would not be there in your bean.

okay, I just stole some time to verify the array thing. Suppose you have 2 texboxes with same name, say myTxt, then they are mapped to an array myTxt provided there is no index violation.

While mapping this to the form beans, the method public void setMyTxt(String[]) would be called. here you can assign the reference accordingly. I checked this and it is working fine. However to get this value back you need use the array's index as I said earlier.

Since myTxt is an array, you cannot print myTxt, but myTxt[0] or some other valid index.

for the <html:text... teg you need to use EL.

Regards
 
Himanshu Kansal
Ranch Hand
Posts: 257
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
${stockID[0]}
${stockID[1]}...
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic