• 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
  • Jeanne Boyarsky
  • Ron McLeod
Sheriffs:
  • Paul Clapham
  • Liutauras Vilda
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
Bartenders:

how to change select menu dynamically in a JSP

 
Ranch Hand
Posts: 197
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I would like to have the JSP view, change a select option dynamically based on the returned request parameter from a bean. I know that to do this with a text field, simply place the variable in the input value, but how about with select or with radio menu??
 
Ranch Hand
Posts: 60
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
One way, maybe not the best.

Include in your bean a session object for earch return parameter.

in your jsp search to see if that session object != null and display the appropriate select menu for that session object.
 
kwame Iwegbue
Ranch Hand
Posts: 197
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
but in select menu like:



how do you make the select display a certain option when the bean returns to the JSP. I know in static html you can say: selected="selected" but how to do this dynamically?
 
Sheriff
Posts: 67753
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Same way. Determine which one is to be selected and place the appropriate markup on the tag.
 
Al Hollis
Ranch Hand
Posts: 60
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
if(session.getAttribute(Cars) != null)
{
%>

<select name="somename" />
<option value="<%= variable_1 %>" ><%= variable_1 %></option>
<option value="<%= variable_2 %>" ><%= variable_2 %></option>
<option value="<%= variable_3 %>" ><%= variable_3 %></option>
</select>

<%
}
else if(session.getAttribute(bikes) != null)
{
%>

<select name="somename" />
<option value="<%= variable_1 %>" ><%= variable_1 %></option>
<option value="<%= variable_2 %>" ><%= variable_2 %></option>
<option value="<%= variable_3 %>" ><%= variable_3 %></option>
</select>
<% } %>

Selected can then depend on what is stored in the session atribute...

See post below..
[ April 02, 2006: Message edited by: Al Hollis ]
 
Bear Bibeault
Sheriff
Posts: 67753
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have no idea what the previous post is getting at. There is no need to bring the session into this, the HTML is not well formed, and as far as I can tell, the then and else parts of his statement are identical.

All that needs to happen is to stick the selected="selected" on the apporpiate option.
[ April 01, 2006: Message edited by: Bear Bibeault ]
 
Al Hollis
Ranch Hand
Posts: 60
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yeh i was trying to show the fact ( bad coding i know ) That in a jsp you could check for an attribute store in a session and display a different select box for different attributes.

e.g In example. One select menu for cars, one for bikes.

If you want the selected option to be stored you could just do

selected="<% myBean.getCarOne() %> "

Hope thats clearer
 
When it is used for evil, then watch out! When it is used for good, then things are much nicer. Like 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