my dog learned polymorphism*
The moose likes Struts and the fly likes html:text value is not retained on server-side Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Frameworks » Struts
Bookmark "html:text value is not retained on server-side" Watch "html:text value is not retained on server-side" New topic
Author

html:text value is not retained on server-side

Timothy Sam
Ranch Hand

Joined: Sep 18, 2005
Posts: 746
Hi guys

I have an ActionForm which has a String[] member. Let's name it, myVar. Now, myVar is being poppulated in one of my action class before page display.



and is displayed in my JSP like this (sorry for the scriptlet, I'm just maintaining the app)



The values are being displayed fine... Now, when I change say, the value of myVar[0] textbox in HTML, on my Action class the value of myVar[0] is not changed.

I did my ActionForm like this but still didn't work...



Thanks!


SCJP 1.5
http://devpinoy.org/blogs/lamia/ - http://everypesocounts.com/
Merrill Higginson
Ranch Hand

Joined: Feb 15, 2005
Posts: 4864
The problem is with this statement in your ActionForm:

Since you never initialize the String array, you can never set any of its elements because it doesn't have any.

The quick and dirty way to get around this would be to initialize the array with the size being some arbitrary value you don't expect to be exceeded, and just deal with the fact that some elements in the array will be null and hope the actual size never exceeds the value you set.

Two better solutions would be:
  • Carry forward the array size as a hidden field in the JSP and then override the reset(...) method of the Action form and have it retrieve the value from the request and initialize the array to that size
  • Use a List instead of an array and use "lazy initialization" techniques in your indexed getter and setter to add elements to the List if they're not already there.

  • [ July 11, 2008: Message edited by: Merrill Higginson ]

    Merrill
    Consultant, Sima Solutions
    Timothy Sam
    Ranch Hand

    Joined: Sep 18, 2005
    Posts: 746
    thanks merrill

    I haven't got the chance to try your solution yet. One more question though... So even though I came from an action, initialized the array there before entering the JSP, I still won't be able to get the values?

    Thank you!
    Merrill Higginson
    Ranch Hand

    Joined: Feb 15, 2005
    Posts: 4864
    Assuming your ActionForm is in request scope, The instance of the ActionForm that is created when the form is submitted is a different instance than was used at the time the JSP was rendered, which is now out of scope. That's why you have to initialize the array in the ActionForm.
     
    GeeCON Prague 2014
     
    subject: html:text value is not retained on server-side