This week's book giveaway is in the Reactive Progamming forum.
We're giving away four copies of Reactive Streams in Java: Concurrency with RxJava, Reactor, and Akka Streams and have Adam Davis on-line!
See this thread for details.
Win a copy of Reactive Streams in Java: Concurrency with RxJava, Reactor, and Akka Streams this week in the Reactive Progamming forum!
  • 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Junilu Lacar
  • Jeanne Boyarsky
  • Bear Bibeault
Sheriffs:
  • Knute Snortum
  • Tim Cooke
  • Devaka Cooray
Saloon Keepers:
  • Ron McLeod
  • Stephan van Hulst
  • Tim Moores
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Piet Souris
  • Frits Walraven
  • Ganesh Patekar

Problem in setting a value of drop down

 
Ranch Hand
Posts: 572
  • Mark post as helpful
  • send pies
  • 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
  • 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
  • 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
  • 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
  • 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.
 
Politics is a circus designed to distract you from what is really going on. So is this tiny ad:
Java file APIs (DOC, XLS, PDF, and many more)
https://products.aspose.com/total/java
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!