File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes JSP and the fly likes JSP Drop Down List Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » JSP
Bookmark "JSP Drop Down List" Watch "JSP Drop Down List" New topic
Author

JSP Drop Down List

Michael James Payne
Greenhorn

Joined: Jan 29, 2004
Posts: 9
How do I mainitain the selected value from the dropdown list so when the jsp is redisplayed, the selected values are maintained (are shown) and not lost?



Michael J. Payne
Consultant
Fusion Alliance
Indianapolis, Indiana


Michael J. Payne
Stefan Evans
Bartender

Joined: Jul 06, 2005
Posts: 1016
You have to generate appropriate HTML to indicate which records in the list are selected.
Basically for whichever option is selected, you have to have <option value="42" selected="true">

Frameworks like struts/JSF will often take care of this for you, otherwise you do it manually, with an if statement in the middle of your rendering the list.
Another approach is to render the list as usual, and then execute a javascript command onload which sets the value. It sometimes looks a bit neater in code ;-)
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60780
    
  65

One caveat:

selected="true"

is probably a poor way to express this because it would lead one to believe that saying selected="false" would have the opposite effect.

In XHTML, if anything is supplied as a value to the selected attribute -- be it selected="true" or selected="false" or even selected="sdhsjhdsh" -- the option will be selected.

This is true of all attributes that have no value in HTML but must have a value in XHTML.

The standard means of expressing these is to repeat the name of the attribute. E.g. selected="selected".
[ October 19, 2006: Message edited by: Bear Bibeault ]

[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
toukhir mujawar
Ranch Hand

Joined: Oct 11, 2006
Posts: 70
hi...

just try this one...

<option value="1" selected>

this is sufficient... need not write select= "true"/false/anything else...
reddy yeruva
Greenhorn

Joined: Oct 18, 2006
Posts: 18
ok if you want the selected value in the page after refresh means again you are sending it to the same page yes..

simple

place in javascript as

lacation="pagename.jsp?&yourattribute=value"

simple


Y.Tirupati Reddy<br />reddy.yeruva@gmail.com
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60780
    
  65

Originally posted by toukhir mujawar:
need not write select= "true"/false/anything else...


Depends upon whether you are using HTML or XHTML. In the latter, the value is required.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: JSP Drop Down List
 
Similar Threads
multi select list in struts jsp.
Populating a value based on the option selected in the dropdown
Struts 1.1: Help needed with html:select, html:options, html:optionsCollection
Pre-populate Mulitple Select HTML Tag
MULTIPLE select - list box - how to read in servlet