【发布时间】:2011-07-25 21:30:35
【问题描述】:
我需要将枚举的所有值显示为<option> 元素。我使用 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()));
}
%>
...
但是,我想改用 JSTL/EL 代码来实现它。我该怎么做?
更新:
Spring 现在有一个更简单的方法来做到这一点。首先添加弹簧框架工作标签
<%@ taglib prefix="form" uri="http://www.springframework.org/tags/form" %>
那么如果你只是声明一个选择,其中路径中的变量是一个枚举,
spring 自动查找其他元素。
<form:select path="dataFormat.delimiter" class="dataFormatDelimiter">
<form:options items="${dataFormat.delimiter}"/>
</form:select>
【问题讨论】:
-
@BalusC 感谢您的编辑现在更清晰了。