• Post Reply Bookmark Topic Watch Topic
  • New Topic

how to change select menu dynamically in a JSP  RSS feed

 
kwame Iwegbue
Ranch Hand
Posts: 197
  • Mark post as helpful
  • send pies
  • 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??
 
Al Hollis
Ranch Hand
Posts: 60
  • Mark post as helpful
  • send pies
  • 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
  • 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?
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 65833
134
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • 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
  • 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
Author and ninkuma
Marshal
Posts: 65833
134
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • 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
  • 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
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!