Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Drop downs and getting selected value

 
Andrew Mcmurray
Ranch Hand
Posts: 188
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi all

What is the best way to redisplay a previously selected value in a drop down? For example I have a drop down and the user selects a value. I store that value in a bean. When the user returns to the page I want that value to be the selected one in the drop down. Right now I am doing this which is working, but I don't think it is the right way because I can't default to anything now. I am going to have to do the same thing on a multi select also.



Thanks,

AMD
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64700
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Be sure that the selected value is available to the page and use an if condition to apply the attribute selected="selected" to the option that is to be selected.

This has been covered before so search through this forum for previous examples.
 
Andrew Mcmurray
Ranch Hand
Posts: 188
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Bear I'll look for search for examples. I also need to do the same for multi selects are there also examples for this?
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64700
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Same markup.
 
Andrew Mcmurray
Ranch Hand
Posts: 188
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Bear,

For the multi selects I saw a lot of examples. Is it just not an easy way like in struts to do this? I know what the option values are so I think it is cluncky to have to create an array of the values, then loop over them seeing if one is in my array of selected and then setting it as selected if it is?

Thanks,

AMD
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64700
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How are you getting the values to the page if not in some sort of collection?

And, if you want a handy-dandy easy way to do it, define a custom action that will take care of all the gnarly details for you.
 
Andrew Mcmurray
Ranch Hand
Posts: 188
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Bear,

Yes the selected ones are put in an array list and then put on the request, but all the other ones are just static and were in the html. From the examples I saw you need two loops one to go through all the possible and one to see if it was selected. I guess some JSTL will make it a little better. When you say custom action, do you mean some kind of custom tag?

Thanks,

AMD
 
Christophe Verré
Sheriff
Posts: 14691
16
Eclipse IDE Ubuntu VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
do you mean some kind of custom tag

Yes, I think he does.
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64700
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
As of JSP 2.0 what used to be called the standard and custom "tags" are now known as "actions".

If your select and its options are static markup in the JSP, you're kind of sunk as far as dynamically setting the selceted value. Your best bet in that case might be to emit some Javascript that sets the selection upon page load.
 
Andrew Mcmurray
Ranch Hand
Posts: 188
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Cool thanks guys

AMD
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic