Selected value for JSP drop down using JSTL

后端 未结 7 685
無奈伤痛
無奈伤痛 2020-11-29 04:37

I have SortedMap in Servlet to populate drop down values in JSP and I have the following code

    SortedMap dept = findDepartment();
           


        
相关标签:
7条回答
  • 2020-11-29 05:01

    In HTML, the selected option is represented by the presence of the selected attribute on the <option> element like so:

    <option ... selected>...</option>
    

    Or if you're HTML/XHTML strict:

    <option ... selected="selected">...</option>
    

    Thus, you just have to let JSP/EL print it conditionally. Provided that you've prepared the selected department as follows:

    request.setAttribute("selectedDept", selectedDept);
    

    then this should do:

    <select name="department">
        <c:forEach var="item" items="${dept}">
            <option value="${item.key}" ${item.key == selectedDept ? 'selected="selected"' : ''}>${item.value}</option>
        </c:forEach>
    </select>
    

    See also:

    • How can I retain HTML form field values in JSP after submitting form to Servlet?
    0 讨论(0)
  • 2020-11-29 05:03

    Maybe I don't completely understand the accepted answer so it didn't work for me.

    What i did was simply to check if the variable is null, assign it to a known value from my database. Which seems to be similar to the accepted answer whereby you first declare an known value and set it to selected

    <select name="department">
        <c:forEach var="item" items="${dept}">
            <option value="${item.key}">${item.value}</option>
        </c:forEach>
    </select>
    

    because none of the options are selected, thus item = null

    <%
        if(item == null){
            item = "selectedDept"; //known value from your database
        }
    %>
    

    This way if the user then selects another option, my IF clause will not catch it and assign to the fixed value that was declared at the start. My concept could be wrong here but it works for me

    0 讨论(0)
  • 2020-11-29 05:09

    I tried the accepted answer, it did not work.

    However the simple way to do it is below:-

    <option value="1" <c:if test="${item.quantity == 1}"> <c:out value= "selected=selected"/</c:if>>1</option>
    <option value="2" <c:if test="${item.quantity == 2}"> <c:out value= "selected=selected"/</c:if>>2</option>
    <option value="3" <c:if test="${item.quantity == 3}"> <c:out value= "selected=selected"/</c:if>>3</option>
    

    Enjoy!!

    0 讨论(0)
  • 2020-11-29 05:10

    i tried the last answer from Sandeep Kumar, and i found way more simple :

    <option value="1" <c:if test="${item.key == 1}"> selected </c:if>>
    
    0 讨论(0)
  • 2020-11-29 05:10

    I think above examples are correct. but you dont' really need to set

    request.setAttribute("selectedDept", selectedDept);
    

    you can reuse that info from JSTL, just do something like this..

    <!DOCTYPE html>
    <html lang="en">
    <%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
    <%@taglib prefix="fn" uri="http://java.sun.com/jsp/jstl/functions" %>
    <head>
        <script src="../js/jquery-1.8.1.min.js"></script>
    </head>
    <body>
        <c:set var="authors" value="aaa,bbb,ccc,ffffd,eee,fff,ggg" scope="application" />
        <c:out value="Before : ${param.Author}"/>
        <form action="TestSelect.action">
            <label>Author
                <select id="Author" name="Author">
                    <c:forEach items="${fn:split(authors, ',')}" var="author">
                        <option value="${author}" ${author == param.Author ? 'selected' : ''}>${author}</option>
                    </c:forEach>
                </select>
            </label>
            <button type="submit" value="submit" name="Submit"></button>
            <Br>
            <c:out value="After :   ${param.Author}"/>
        </form>
    </body>
    </html>
    
    0 讨论(0)
  • You can try one even more simple:

    <option value="1" ${item.quantity == 1 ? "selected" : ""}>1</option>
    
    0 讨论(0)
提交回复
热议问题