java 在 JSTL 中使用枚举
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6739918/
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
Using enum in JSTL
提问by Suli
I am trying to do some website development using jstl and I run into the following problem:
我正在尝试使用 jstl 进行一些网站开发,但遇到以下问题:
Here I am trying to create a dropdown, where the displayed value is the country names, and the value is the country code. To do this I have the following enum in the backend java code:
在这里,我试图创建一个下拉列表,其中显示的值为国家名称,值为国家代码。为此,我在后端 java 代码中有以下枚举:
public static enum CountryCodes implements EnumConstant {
USA, CAN, AUS, GBR, DEU, ESP, GUM, IND, ISR, MEX, NZL, PAN, PRI;
public final String toCountry(){
switch(this){
case USA:
return "United States";
case CAN:
return "Canada";
case AUS:
return "Australia";
case GBR:
return "Great Britan";
case DEU:
return "Germany";
case ESP:
return "Spain";
case GUM:
return "Guam";
case IND:
return "India";
case ISR:
return "Isreal";
case MEX:
return "Mexico";
case NZL:
return "New Zealand";
case PAN:
return "Panama";
case PRI:
return "Puerto Rico";
}
return this.toString();
}
}
And the jsp code snippet is like the following:
而jsp代码片段如下:
<c:set var="countryCodes" value="<%=RequestConstants.CountryCodes.values()%>" />
<td>
<select id="<%=RequestConstants.CLModifyPage.COUNTRY_CODE%>"
name="<%=RequestConstants.CLModifyPage.COUNTRY_CODE%>">
<c:forEach items="${countryCodes}" var="countryCode">
<c:choose>
<c:when
test="${sessionScope.CURRENT_INSTITUTION.countryCode == countryCode}">
<option value="${countryCode}" selected="selected">
${countryCode.toCountry()}</option>
</c:when>
<c:otherwise>
<option value="${countryCode}">${countryCode.toCountry()}
</option>
</c:otherwise>
</c:choose>
</c:forEach>
</select>
</td>
But the above code has two problems:
但是上面的代码有两个问题:
countryCode.toCountry()
doesn't really work... I am not sure what syntax it should be.if
"${sessionScope.CURRENT_INSTITUTION.countryCode}"
is not a valid enum value, i.e, if it's something like "AAA", then the comparison fails and throws an java.lang.IllegalArgumentException: no enum const CountryCodes.AAA defined. How can I get around that?
countryCode.toCountry()
并没有真正起作用......我不确定它应该是什么语法。如果
"${sessionScope.CURRENT_INSTITUTION.countryCode}"
不是有效的枚举值,即,如果它类似于“AAA”,则比较失败并抛出 java.lang.IllegalArgumentException: no enum const CountryCodes.AAA defined。我怎样才能解决这个问题?
回答by BalusC
Your approach is too complicated.
你的方法太复杂了。
Redesign your enum as follows:
重新设计你的枚举如下:
public enum CountryCode {
USA("United States"),
CAN("Canada"),
AUS("Australia");
// ...
private String label;
private CountryCode(String label) {
this.label = label;
}
public String getLabel() {
return label;
}
}
(note that it has now a fullworthy and more efficient getter!)
(请注意,它现在有一个完整且更高效的 getter!)
Store the enum values in the application scope during servlet's init()
method or, better, during ServletContextListener
's contextInitialized()
method:
servlet的过程中存储在应用程序范围的枚举值init()
的方法,或者更好的,期间ServletContextListener
的contextInitialized()
方法:
servletContext.setAttribute("countryCodes", CountryCode.values());
Finally traverse it as follows:
最后遍历如下:
<select name="countryCode">
<c:forEach items="${countryCodes}" var="countryCode">
<option value="${countryCode}" ${institution.countryCode == countryCode ? 'selected' : ''}>${countryCode.label}</option>
</c:forEach>
</select>
回答by nemecek
If you are using spring you can
如果您使用的是弹簧,则可以
<form:select path="_path" >
<spring:eval expression="T(com.EnumName).values()" var="_enum"/>
<c:forEach items="${_enum}" var="_value">
<form:option value="${_value}" label="${_value.label}"/>
</c:forEach>
</form:select>