This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes Struts and the fly likes Autopupulate of the jsp page using values from th FormBean Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Frameworks » Struts
Bookmark "Autopupulate of the jsp page using values from th FormBean" Watch "Autopupulate of the jsp page using values from th FormBean" New topic
Author

Autopupulate of the jsp page using values from th FormBean

Velika Srbija
Ranch Hand

Joined: Jan 15, 2002
Posts: 172
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?
Purushoth Thambu
Ranch Hand

Joined: May 24, 2003
Posts: 425
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?
Velika Srbija
Ranch Hand

Joined: Jan 15, 2002
Posts: 172
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.:

<html:select property="producer_name">
<html ption value="ford">Ford</html ption>
<html ption value="peugeot">Peugeot</html ption>
<html ption value="fiat">Fiat</html ption>
</html:select>

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?
Dom Lassy
Ranch Hand

Joined: May 05, 2006
Posts: 181
Instead of using a scriptlet to display your options, use htmlptionsCollection.
[ February 12, 2007: Message edited by: Dom Lassy ]
Merrill Higginson
Ranch Hand

Joined: Feb 15, 2005
Posts: 4864
Also, don't forget that since "list_of_producers" is in request scope, you must rebuild that list in your Action class. Otherwise, it's out of scope when the JSP is called the second time.


Merrill
Consultant, Sima Solutions
Velika Srbija
Ranch Hand

Joined: Jan 15, 2002
Posts: 172
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.
Merrill Higginson
Ranch Hand

Joined: Feb 15, 2005
Posts: 4864
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 ]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Autopupulate of the jsp page using values from th FormBean
 
Similar Threads
update html:options
selecting a value in drop down box
html:select problem-------
whats problem with following code ** urgent
passing two arrays to Javascript function