Results 1 to 3 of 3
  1. #1
    mishy is offline Member
    Join Date
    Feb 2014
    Posts
    8
    Rep Power
    0

    Default Help with JSP form drop down list

    I have a jsp form that has a drop down option for a user to select. I would like to know how to have a pre-selected option that has been retrieved from my database or provide a default option if none has been selected by the user. My form allows a user to update a record in my database and what I need to achieve is when a user is updating an existing record they do not have to touch the drop down box if the do not need to change that option. Here is my current form below
    Java Code:
     form method="POST" action='UserController?action=edit&albumId=${album.albumId}' name="frmAddAlbum">
    		<label for="album id">Format : </label> 
    		<select name="action" selected=<c:out value="${album.format}" /> >
    			<option value="listUser">CD</option>
    			<option value="listUser">Tape</option>
    			<option value="listUser">MP3</option>
    			<option value="listUser">VINYL</option>
    			</select>
    			 <br /> 
    			 <label for="album id">Album ID : </label> <input type="text" readonly="readonly" name="albumID"
                value="<c:out value="${album.albumId}" />" class="input" /> <br /> 
    			 <label
    			for="artistName">Artist Name : </label><input type="text"
    			name="artistName" value="<c:out value="${album.artistName}" />"
    			class="input" /> <br /> <label for="Album name">Album Name
    			: </label><input type="text" name="albumName"
    			value="<c:out value="${album.albumName}" />" class="input" /> <br />
    		<label for="yearReleased"> Year Released : </label><input type="text"
    			name="yearReleased"
    			value="<fmt:formatDate pattern="MM/dd/yyyy" value="${album.yearReleased}" />"
    			class="input" /> <br /> <label for="Email">Song : </label><input
    			type="text" name="songs" value="<c:out value="${album.songs}" />"
    			class="input" /> <br/>
    			<label for="album id">Ratings : </label> <select name="action">
    			<option value="listUser">1</option>
    			<option value="listUser">2</option>
    			<option value="listUser">3</option>
    			<option value="listUser">4</option>
    			<option value="listUser">5</option>
    			</select><br /> 
    			
    			 <br /> <input type="submit" value="Submit" />
    	</form>

  2. #2
    sehudson's Avatar
    sehudson is offline Senior Member
    Join Date
    Mar 2010
    Posts
    356
    Rep Power
    5

    Default Re: Help with JSP form drop down list

    As your code stands now, CD should be the option selected by default since it is the first option in your select dropdown.

    As far as having the options coming from the database, is the user forwarded to this JSP from a Servlet?
    If so, you can do the work of retrieving the list of drop down options in your servlet, then add the list as an attribute in your request.

    From there, you can populate the dropdown using a JSTL for loop, and iterate over the list of options, adding an <option> for each one.

    As far as selecting a particular option by default, I believe the first item that you add is going to be selected by default, so that is going to be driven by the order that the options are in the List. If you want to have a specific option be selected, you probably want to address this in the control layer(the servlet). Once you retrieve the list of options that you want to display, execute your logic for determining which option should be selected, and re-arrange your list accordingly, then add the list as an attribute in your request, something like:

    Java Code:
    ...
    List listOfOptions = retrieve the options from the database
    ...execute your logic for determining the order that the options should fall in, which should come first, etc, re order the list
    request.setAttribute("listOfOptions", listOfOptions );
    ...
    forward on to jsp
    That way, in the JSP you just iterate over the list of options normally, and the JSP doesn't have to be concerned about the order that the options are in, since you will have already addressed that in the servlet.

    That is how I would approach it.
    Last edited by sehudson; 05-28-2014 at 07:34 PM.

  3. #3
    mishy is offline Member
    Join Date
    Feb 2014
    Posts
    8
    Rep Power
    0

    Default Re: Help with JSP form drop down list

    Thanks for the well detailed response. I have managed to get some more help with getting to work exactly how I expected it to work like. The solution was

    Java Code:
    <select name="rating">
    				<option value="1" ${album.rating == 1 ? 'selected' : ''}>1</option>  
    				<option value="2" ${album.rating == 2 ? 'selected' : ''}>2</option>  
    				<option value="3" ${album.rating == 3 ? 'selected' : ''}>3</option>  
    				<option value="4" ${album.rating == 4 ? 'selected' : ''}>4</option>  
    				<option value="5" ${album.rating == 5 ? 'selected' : ''}>5</option>  
    			</select>

Similar Threads

  1. 4 dependent drop down list
    By deena in forum Advanced Java
    Replies: 1
    Last Post: 11-30-2012, 01:25 PM
  2. drop down list
    By nour in forum Android
    Replies: 2
    Last Post: 11-20-2011, 09:25 PM
  3. what code for searching in drop down list?
    By Harmesh Goyal in forum Advanced Java
    Replies: 1
    Last Post: 03-03-2011, 04:43 AM
  4. Replies: 0
    Last Post: 01-05-2011, 07:28 AM
  5. JComboBox and drop-down list.....HELP!!!!!
    By Anna in forum AWT / Swing
    Replies: 1
    Last Post: 06-18-2008, 05:16 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •