• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • paul wheaton
  • Liutauras Vilda
  • Ron McLeod
Sheriffs:
  • Jeanne Boyarsky
  • Devaka Cooray
  • Paul Clapham
Saloon Keepers:
  • Scott Selikoff
  • Tim Holloway
  • Piet Souris
  • Mikalai Zaikin
  • Frits Walraven
Bartenders:
  • Stephan van Hulst
  • Carey Brown

Problem in setting a value of drop down

 
Ranch Hand
Posts: 572
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

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?
 
pie sneak
Posts: 4727
Mac VI Editor Ruby
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Display the text and include an html:hidden for the value.
 
Ranch Hand
Posts: 4864
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Use the <html:select> tag.

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

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

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 ]
 
Ali Gohar
Ranch Hand
Posts: 572
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

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
Posts: 4864
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
He puts the "turd" in "saturday". Speaking of which, have you smelled this tiny ad?
We need your help - Coderanch server fundraiser
https://coderanch.com/wiki/782867/Coderanch-server-fundraiser
reply
    Bookmark Topic Watch Topic
  • New Topic