This week's book giveaway is in the Mac OS forum.
We're giving away four copies of a choice of "Take Control of Upgrading to Yosemite" or "Take Control of Automating Your Mac" and have Joe Kissell on-line!
See this thread for details.
The moose likes Struts and the fly likes Arrays in struts ActionForm showing '[Ljava.lang.String;@10321032' Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Frameworks » Struts
Bookmark "Arrays in struts ActionForm showing Watch "Arrays in struts ActionForm showing New topic
Author

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

John Zwick
Ranch Hand

Joined: Mar 02, 2009
Posts: 34
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

Joined: Aug 03, 2008
Posts: 9303
    
  17

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...


SCJP 6 | SCWCD 5 | Javaranch SCJP FAQ | SCWCD Links
John Zwick
Ranch Hand

Joined: Mar 02, 2009
Posts: 34
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

Joined: Jul 05, 2009
Posts: 257
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.


Experience and talent are independent of age
John Zwick
Ranch Hand

Joined: Mar 02, 2009
Posts: 34
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

Joined: Jul 05, 2009
Posts: 257
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

Joined: Jul 05, 2009
Posts: 257
${stockID[0]}
${stockID[1]}...
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Arrays in struts ActionForm showing '[Ljava.lang.String;@10321032'