File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Struts and the fly likes Problem in setting a value of drop down Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Frameworks » Struts
Bookmark "Problem in setting a value of drop down" Watch "Problem in setting a value of drop down" New topic

Problem in setting a value of drop down

Ali Gohar
Ranch Hand

Joined: Mar 18, 2004
Posts: 572

I have following scenerio:

I have a JSP page with 1 text box like:
<html:text property="testVal" />

I have linked a form with this JSP page to hold the property and defined the getters and setters.

Now on submitting the form, an action is being called and i pull the data from DB depending upon the value received from the FORM.

I put all the values got from the DB to a bean and set this bean to Request scope and then forward to another JSP to display these values.

In that JSP i display values as read only but for one value i have to show a drop down list and depending upon one value got from DB, i have to select the text in that drop down. For Example if i have drop down list with values 1,2,3 and i get value 2 from DB, then i want to select the text against value 2.

So how can i achieve that? As it is required by STRUTS that to select a specific value, we need to set it in the form. But in this case how can i set the value in the FORM? I have attached a form with this later JSP too but in that previous action where i pull the data from DB i have the instance of other form with 1 value.

Any body knows?
Marc Peabody
pie sneak

Joined: Feb 05, 2003
Posts: 4727

Display the text and include an html:hidden for the value.

A good workman is known by his tools.
Merrill Higginson
Ranch Hand

Joined: Feb 15, 2005
Posts: 4864
Use the <html:select> tag.

For example, if you put the following in your jsp:

<html:select property="x">
<htmlptionsCollection property="list" />

Struts will populate the options of the dropdown by calling getList() on your form bean, which should return an array or collection of values. Struts will then use the value returned by getX() to decide which option is selected. For example, if getList() returns 1, 2, and 3 and getX()returns 2, option 2 will display as selected.
[ November 01, 2005: Message edited by: Merrill Higginson ]

Consultant, Sima Solutions
Ali Gohar
Ranch Hand

Joined: Mar 18, 2004
Posts: 572

I know that and I have defined that <html:select> the same way but how can i set the property of that form bean as i don't get that form bean instance in the action. I get the instance of previous form which is submitted to the action.

Please advice
Merrill Higginson
Ranch Hand

Joined: Feb 15, 2005
Posts: 4864
As long as you have created a bean that extends ActionForm, you can instantiate that bean any time you want, populate it's properties, and put it in the desired scope. The solution, then is in the Action class that is called by the first page, instantiate the form bean used by the second page, populate it's properties, and place it in the appropriate scope (request, session, appplication). The second page will then display the values properly.
I agree. Here's the link:
subject: Problem in setting a value of drop down
Similar Threads
Displaying Data From Different Tables in .jsp
Combo box building
Multiple forms - one jsp
html:optionsCollection from a DynaActionForm
Drop down list value