问题
I am trying to do some website development using jstl and I run into the following problem:
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:
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:
<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?
回答1:
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!)
Store the enum values in the application scope during servlet's init()
method or, better, during ServletContextListener's contextInitialized()
method:
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>
回答2:
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>
来源:https://stackoverflow.com/questions/6739918/using-enum-in-jstl