如何将枚举的所有值显示为
我需要将枚举的所有值显示为 元素。我已经使用 scriptlet 实现了这一点:
<%@ 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>
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>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
创建一个
ServletContextListener
实现,在 web 应用程序启动期间将枚举值放入应用程序范围内,以便通过${carrierCodes}
在 EL 中可用。此类可重复用于您在 Web 应用程序启动期间想要执行的所有其他操作。请注意,我使用了
Enum#values()
而不是笨拙的Class#getEnumConstants()
方法。它返回所有枚举值的数组。然后,在 JSP 中,只需使用 JSTL
来迭代它。Create a
ServletContextListener
implementation which puts the enum values in the application scope during webapp startup so that it's available in EL by${carrierCodes}
. This class is reuseable for all other things you'd like to do once during webapp's startup.Note that I used
Enum#values()
instead of the clumsyClass#getEnumConstants()
method. It returns an array of all enum values.Then, in JSP, just use JSTL
<c:forEach>
to iterate over it.