Java JSTL foreach 枚举
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3978551/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
JSTL foreach on enum
提问by jayjaypg22
I have a contant list declared in java using enum type, that must appears in a jsp. Java enum declaration :
我有一个使用 enum 类型在 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>
where as :
然而 :
<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>
Works fine. Is there a limitation on enum types ni foreach loop?
工作正常。对枚举类型 ni foreach 循环是否有限制?
采纳答案by jayjaypg22
The values method works fine, my mistake. Indeed the problem was that I didn't put my list in the page scope of my jsp.
values 方法工作正常,我的错误。事实上,问题是我没有将我的列表放在我的 jsp 的页面范围内。
<% pageContext.setAttribute("monEnum", ConstanteADMD.ListTypeAffichage.values()); %>
...
<c:forEach var="entry" items="${monEnum}">
<option>${entry.type}</option>
</c:forEach>
No need of the getNames
method
不需要getNames
方法
回答by highlycaffeinated
The EL you are using in the items attribute on c:forEach is trying to call a static method on your enum types. I believe that EL only supports calls on instance methods.
您在 c:forEach 上的 items 属性中使用的 EL 试图对您的枚举类型调用静态方法。我相信 EL 只支持调用实例方法。
回答by Shervin Asgari
You can create a method that returns Enum.values()
if you cannot use values directly in your EL expression.
Enum.values()
如果您不能直接在 EL 表达式中使用值,您可以创建一个返回的方法。
Remove the getNames()
from inside your Enum and use a method like this instead somewhere else in your code.
getNames()
从您的 Enum 中删除 ,并在代码中的其他地方使用这样的方法。
public List<LIST_TYPE_AFFICHAGE> getNames() {
return new ArrayList<LIST_TYPE_AFFICHAGE>(Arrays.asList(LIST_TYPE_AFFICHAGE.values()));
}
回答by Steven Benitez
Another option is to use a <c:set/>
tag like such:
另一种选择是使用这样的<c:set/>
标签:
<c:set var="enumValues" value="<%=YourEnum.values()%>"/>
Then just iterate over it as such:
然后像这样迭代它:
<c:forEach items="${enumValues}" var="enumValue">
...
</c:forEach>
Your IDE should prompt you to import the YourEnum
class.
您的 IDE 应该会提示您导入YourEnum
该类。
回答by YROjha
Another simple way can be:
另一种简单的方法可以是:
<c:forEach items="<%=LIST_TYPE_AFFICHAGE.values()%>" var="entry">
<option>${entry.name }</option>
</c:forEach>
You need to import these:
您需要导入这些:
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<%@page import="packagename.LIST_TYPE_AFFICHAGE"%>