I need to display all values of an enum as <option>
elements. I have achieved this using scriptlets:
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<%@ taglib prefix="errors" tagdir="/WEB-INF/tags/jostens/errors" %>
<%@ taglib prefix="fn" uri="http://java.sun.com/jsp/jstl/functions" %>
<%@ taglib prefix="spring" uri="http://www.springframework.org/tags" %>
<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %>
...
<%
Class<?> c = CarrierCode.class;
for (Object carrier : c.getEnumConstants()) {
CarrierCode cc = (CarrierCode) carrier;
StringBuilder sb = new StringBuilder();
Formatter formatter = new Formatter(sb, Locale.US);
out.print(formatter.format("<option value='%s'>%s</option>\n", cc.getMfCode(), cc.name()));
}
%>
...
However, I would like to implement it using JSTL/EL code instead. How can I do it?
UPDATE:
Spring has a much easier way to do this now. First add the spring frame work tags
<%@ taglib prefix="form" uri="http://www.springframework.org/tags/form" %>
then if you just declare a select where the variable in path is an Enum,
spring automagically finds the other elements.
<form:select path="dataFormat.delimiter" class="dataFormatDelimiter">
<form:options items="${dataFormat.delimiter}"/>
</form:select>