jsp-dropdownlist
hi i have one doubt.how t开发者_高级运维o diplay array values inside drop down list
You can use JSTL <c:forEach>
tag for this. If you don't have JSTL installed yet, just drop jstl-1.2.jar in /WEB-INF/lib
. Then, in top of your JSP declare the JSTL code taglib as per its documentation:
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
Then, there where you need to populate the dropdown options, use <c:forEach>
to iterate over an array like String[]
or a collection like List<String>
in the scope. E.g.
<select name="country">
<c:forEach items="${countries}" var="country">
<option value="${country}">${country}</option>
<c:forEach>
</select>
If you want to have separate option value-label pairs, then rather use a Map<String, String>
instead which you can iterate like follows:
<select name="country">
<c:forEach items="${countries}" var="country">
<option value="${country.key}">${country.value}</option>
<c:forEach>
</select>
The ${map.key}
returns the map's key and the ${map.value}
returns the map's value.
精彩评论