jay*_*g22 10 java jsp enumeration jstl
我有一个使用枚举类型在java中声明的内容列表,它必须出现在jsp中.Java枚举声明:
public class ConstanteADMD {
public enum LIST_TYPE_AFFICHAGE {
QSDF("qsmldfkj"), POUR("azeproui");
private final String name;
@Override
public String toString() {
return name;
}
private LIST_TYPE_AFFICHAGE(String name) {
this.name = name;
}
public static List<String> getNames() {
List<String> list = new ArrayList<String>();
for (LIST_TYPE_AFFICHAGE test : LIST_TYPE_AFFICHAGE.values()) {
list.add(test.toString());
}
return list;
}
}
}
<select name="typeAffichage" id="typeAffichage">
<c:forEach var="type" items="${netcsss.outils.ConstanteADMD.LIST_TYPE_AFFICHAGE.names}">
<option value="${type}">${type}</option>
</c:forEach>
</select>
Run Code Online (Sandbox Code Playgroud)
在哪里:
<select name="typeAffichage" id="typeAffichage">
<c:choose>
<c:when test="${catDecla ne null}">
<option
value="<%=catDecla.getCatDecla().getSTypeAffichage()%>"
selected="selected"><%=catDecla.getCatDecla().getSTypeAffichage()%></option>
</c:when>
</c:choose>
<%List<String> list = ConstanteADMD.LIST_TYPE_AFFICHAGE.getNames();
for(String test : list) {
%>
<option value="<%=test%>"><%=test%></option>
<%}%>
</select>
Run Code Online (Sandbox Code Playgroud)
工作良好.枚举类型是否存在限制?
Ste*_*tez 15
另一个选择是使用这样的<c:set/>标签:
<c:set var="enumValues" value="<%=YourEnum.values()%>"/>
Run Code Online (Sandbox Code Playgroud)
然后只是迭代它:
<c:forEach items="${enumValues}" var="enumValue">
...
</c:forEach>
Run Code Online (Sandbox Code Playgroud)
您的IDE应该提示您导入YourEnum该类.
另一种简单的方法可以是:
<c:forEach items="<%=LIST_TYPE_AFFICHAGE.values()%>" var="entry">
<option>${entry.name }</option>
</c:forEach>
Run Code Online (Sandbox Code Playgroud)
您需要导入这些:
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<%@page import="packagename.LIST_TYPE_AFFICHAGE"%>
Run Code Online (Sandbox Code Playgroud)
价值观方法工作正常,我的错误。事实上,问题是我没有将列表放入 jsp 的页面范围中。
<% pageContext.setAttribute("monEnum", ConstanteADMD.ListTypeAffichage.values()); %>
...
<c:forEach var="entry" items="${monEnum}">
<option>${entry.type}</option>
</c:forEach>
Run Code Online (Sandbox Code Playgroud)
不需要getNames方法