Bind arraylist value to JSP dropdown

I have an arraylist with some data. I need to associate this data with a dropdown in the JSP and get the selected index. Can someone help me with this?

+3
source share
1 answer

You can use the JSTL c:forEach to sort the list (if not yet done, simply remove jstl-1.2.jar in /WEB-INF/libto install it).

<select name="item">
    <c:forEach items="${list}" var="item">
        <option value="${item.value}">${item.label}</option>
    </c:forEach>
</select>

This assumes you List<Item>where Itemlook like this:

public class Item {
    private String value;
    private String label;
    // Add/generate c'tors, getters and setters.
}

On the server side, you can get the selected item as follows:

String selectedItem = request.getParameter("item");

Map<String, String> List<Item>, Item -. :

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

:


:. , Tomcat/lib/*.*, webapp /WEB-INF/lib - ( ) classpath (, JRE/lib/ext). , , - . . /WEB-INF/lib .

, , . /WEB-INF/lib . classpath. , IDE , Eclipse, : Servers, webapp . - . - " Runtimes" . , Eclipse .

+5

Source: https://habr.com/ru/post/1754351/


All Articles