我可以使用 EL 从 JSP 访问枚举类的值吗?
我有一个枚举类USState
。我想遍历 JSP 中的状态。
是否可以访问 USState
列表而不首先将此类列表设置为属性?似乎像枚举这样的静态东西应该始终可用,但我不知道如何做到这一点。
这就是我正在寻找的:(工作除外)
<c:forEach var="state" items="${USState.values}" >
<option value="${state}">${state}</option>
</c:forEach>
I have an enum class USState
. I would like to iterate through the states in a JSP.
Is it possible to access a list of USState
s without first setting such a list as an attribute? It seems that something as static as an enum should always be available, but I can't figure out how to do it.
Here's what I'm looking for: (except working)
<c:forEach var="state" items="${USState.values}" >
<option value="${state}">${state}</option>
</c:forEach>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您还可以考虑将其包装在 Javabean 中,如下所示:
这样就可以通过
访问它:You can also consider to wrap it in a Javabean like follows:
This way it's accessible by
<jsp:useBean>
:您必须在支持代码的某处创建一个列表并将其作为模型参数传递。最好在
ServletContextListener
中(按照 BalusC 的建议)并将其放入ServletContext
(即应用程序范围)中:You will have to create a list somewhere on your backing code and pass it as a model parameter. Preferably in an
ServletContextListener
(as advised by BalusC) and put it in theServletContext
(i.e. application scope):请注意,您还可以使用 scriptlet(我认为在如此简单的情况下它不会太有害):(
USState
应该完全限定或使用<%@ page import 导入=“...”%>
Note that you can also use a scriptlet (I don't think it's too harmful in such a simple case):
(
USState
should be either fully qualified or imported using<%@ page import = "..." %>