I have a problem with autopolulate function of Struts forms. For example, I have in my jsp page the following constructs:
In my DynaActionForm I have a property:
When I access jsp page for the first time, everything is OK. I can see my drop-down list, choose a value and send it to the Action. Struts populates correct value in the producer_name property of the FormBean, but the problem happens when I try to get back to this jsp page from my action. What I get is a mess of various characters, i.e. I don't get my jsp page filled correctly with old values contained in my FormBean. This means that autopopulate of the jsp page using values from the FormBean doesn't work in this case. It relates to previos use of the for loop. How can I overcome this problem?
It's not quite clear what your problem is, Where do you see the wrong values? What is the flow? Is it jsp1 -> action -> jsp1?
What value is stored in the arraylist? Dump the values stored in the list_of_producers at the JSP or in System out stream and check the values. Is the forward from action a redirect?
Joined: Jan 15, 2002
Yes, the flow is: jsp1 -> action -> jsp1
Forward is like: return new ActinForward(mapping.getInput()) But, the same path is for success.
ArrayList is OK.
The problem lies in the fact that when the flow returns to the jsp1 again, it should set the already choosen value in the combo-box (property "producer_name"); that is autopupulating of the jsp1 with values contained in the associated ActionForm. But that doesn't happen.
I have tried to enter static values into combo-box, e.g.:
and in this case, autopopulating works as expected. If I e.g. choose Fiat, when the flow returns me to the jsp1, Fiat will be preselected in the combo-box, and that's OK. But this doesn't work if I dinamically create combo-box values. Why, and what is the solution?
Yes that was the problem. I put it into the session, and my jsp1 page displayed correctly. But, that is only the part of the problem. I don't get messy jsp1 page anymore, but there is no preselected value in the combo-box. Instead, combo-box is rendered as is, i.e. like at the very first visit of the jsp1 page.
Joined: Feb 15, 2005
Your method of populating the options is a little strange, and I'm guessing that may be the problem.
The easiest way to do this is to use the Struts LabelValue bean. Instantiate a bean for each valid option, with the value property being the value of the option and the label property being the label you want displayed. Then put each bean instance into a List stored in session scope.
Then (as Dom already suggested) use the <htmlptionsCollection> tag to populate the options. Your JSP code would look something like this:
See the heading "optionsCollection" in this link for more information on this tag. [ February 12, 2007: Message edited by: Merrill Higginson ]