问题描述
我有一个枚举类 USState
。我想在JSP中遍历州。
I have an enum class USState
. I would like to iterate through the states in a JSP.
是否可以访问 USState
s的列表没有首先设置这样的列表作为属性?似乎一个静态的枚举应该永远是可用的,但是我不知道该怎么做。
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>
推荐答案
你必须在你的背景上创建一个列表代码并将其作为模型参数传递。最好在一个 ServletContextListener
(由BalusC建议),并将它放在 ServletContext
(即应用范围)中: p>
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 the ServletContext
(i.e. application scope):
servletContext.setAttribute("statesList", YourEnum.values());
这篇关于我可以使用EL从JSP访问枚举类的值吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!