aspose file tools*
The moose likes JSP and the fly likes setting the selected option in a select element Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Soft Skills this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Java » JSP
Bookmark "setting the selected option in a select element" Watch "setting the selected option in a select element" New topic
Author

setting the selected option in a select element

kasi Raju
Greenhorn

Joined: Jun 07, 2011
Posts: 8
Hi Every one,

I have a drop-down box in a JSP, when user selects a value and submits want to display values in the same page.
With the drop down value selected .

when I am trying to this I can display drop-down values and when submitted can get the results but not the drop down values.

Thanks in advance.
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61755
    
  67

Basic HTML: set the selected attribute on the <option> element to be shown as selected.


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
kasi Raju
Greenhorn

Joined: Jun 07, 2011
Posts: 8
Thank you for the reply

But how can I do that in JSP page as I am not getting the vales of drop down at all.

After the page submitting getting only results
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61755
    
  67

Well, first you need to know which value you want to be selected. I assume you can get that information from the parameters of the page submission.

Then you can use either the <c:if> JSTL tag to decide whether each option needs the selected attribute or not, or you could use the ternary operator of the EL (the ? operator).
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61755
    
  67

P.S. I have adjusted the topic title to better reflect the content.

P.P.S. This has been discussed before. Search this forum for previous discussions.
Ramkumar Subburaj
Ranch Hand

Joined: Sep 07, 2007
Posts: 83
One more way is, for the option tags, set the id as value.

Use jstl to check if the selected value is available in parameter.

If available, create a script immediately after </select> and do



SCJP 1.5, SCWCD 1.4.
Hanging between Web Services and EJB
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61755
    
  67

Non-optimum as that would not work if script is disabled. It's best to set the value on the server rather than wait for the client to do it.
Ramkumar Subburaj
Ranch Hand

Joined: Sep 07, 2007
Posts: 83
agreed. If javascript is disabled, Most of today's web sites wont work i guess.
Instead of checking whether to set selected or not for each options, imagine there are 100 options, I beleive javascript works better
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61755
    
  67

Ramkumar Subburaj wrote:agreed. If javascript is disabled, Most of today's web sites wont work i guess.

This may be true, but there are still people who disable script thinking it makes them more secure and many shops need to account for this.

Instead of checking whether to set selected or not for each options, imagine there are 100 options, I beleive javascript works better

Disagree. What does the number of options have to do with it in the first place?
Ramkumar Subburaj
Ranch Hand

Joined: Sep 07, 2007
Posts: 83
if the user has selected one option out of 100, in the server side to set the user selected option, you have to check for each option whether it is the one user has selected or not using JSTL and set the selected attribute.

Is there any other way?
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61755
    
  67

I don't see the problem with checking each option -- especially when the options are usually populated by a loop.

I rarely do anything on the client that can be better done on the server.
kasi Raju
Greenhorn

Joined: Jun 07, 2011
Posts: 8
Thank You for the reply

I tried but couldn't get

here is my JSP page


how to retain the value

Thanks
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18985
    
    8

Bear Bibeault, back on June 10, wrote:
Then you can use either the <c:if> JSTL tag to decide whether each option needs the selected attribute or not, or you could use the ternary operator of the EL (the ? operator).


You would put that inside the <option> element at the point where the "selected" attribute would normally go. Presumably you have a way of looking at the "current" object to see whether it should be selected?
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: setting the selected option in a select element