aspose file tools*
The moose likes JSP and the fly likes Drop downs and getting selected value Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » JSP
Bookmark "Drop downs and getting selected value" Watch "Drop downs and getting selected value" New topic
Author

Drop downs and getting selected value

Andrew Mcmurray
Ranch Hand

Joined: Sep 24, 2005
Posts: 188
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

Joined: Jan 10, 2002
Posts: 60991
    
  65

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.


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Andrew Mcmurray
Ranch Hand

Joined: Sep 24, 2005
Posts: 188
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

Joined: Jan 10, 2002
Posts: 60991
    
  65

Same markup.
Andrew Mcmurray
Ranch Hand

Joined: Sep 24, 2005
Posts: 188
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

Joined: Jan 10, 2002
Posts: 60991
    
  65

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

Joined: Sep 24, 2005
Posts: 188
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

Joined: Nov 24, 2005
Posts: 14687
    
  16

do you mean some kind of custom tag

Yes, I think he does.


[My Blog]
All roads lead to JavaRanch
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60991
    
  65

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

Joined: Sep 24, 2005
Posts: 188
Cool thanks guys

AMD
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Drop downs and getting selected value