Struts 2 迭代枚举
Struts 2 中是否可以使用标签
迭代枚举? 现在我正在使用字符串列表来执行此操作,但是是否可以直接使用枚举?
提前致谢。
Is it possible in Struts 2 to iterate an enum using the tag <s:iterator>
?
Right now I'm doing it using a list of String, but is it possible to use an enum directly?
Thanks in advance.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
是的。它有点丑陋,答案是启用静态方法访问,使用 OGNL 表达式的内部类语法(使用“$”),两者结合起来将让您获得值方法,正如史蒂文已经提到的那样。下面是一个示例:
示例操作:
示例 JSP:
输出:
枚举测试
长度:3
一个
两个
三
Note: Ensure that static method access is enabled. Simple way to do this is to add the following after the
<struts>
tag in struts.xml.Yes. It is a bit ugly, the answer is enable static method access, use inner class syntax for the OGNL expression (uses the '$'), both in conjunction will let you then get at the values method as already mentioned by Steven. Here is an example:
Example Action:
Example JSP:
Output:
Enum Test
length: 3
ONE
TWO
THREE
Note: Ensure that static method access is enabled. Simple way to do this is to add the following after the
<struts>
tag in struts.xml.有点像。您不能直接迭代枚举,因为它不是值的集合(枚举引用仅代表枚举常量之一)。但是,您可以迭代枚举(它是一个数组)的
values()
方法,或者您可以在操作中创建一个EnumSet
并迭代它。枚举示例
JSP 示例
Sort of. You can't iterate an enum directly because its not a collection of values (an enum reference just represents one of the enum constants). However, you can iterate the
values()
method of the enum, which is an array, or you can create anEnumSet
in your action and iterate that.Example Enum
Example JSP