Selected value for JSP drop down using JSTL

后端 未结 7 815
無奈伤痛
無奈伤痛 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: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

    
    

    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

提交回复
热议问题