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?