Struts 2 迭代枚举

发布于 2024-11-23 21:08:08 字数 117 浏览 4 评论 0原文

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

爱你不解释 2024-11-30 21:08:08

是的。它有点丑陋,答案是启用静态方法访问,使用 OGNL 表达式的内部类语法(使用“$”),两者结合起来将让您获得值方法,正如史蒂文已经提到的那样。下面是一个示例:

示例操作

package com.action.test;
import com.opensymphony.xwork2.ActionSupport;

public class EnumTest extends ActionSupport{
    enum Numbers{ONE, TWO, THREE};
}

示例 JSP

<%@taglib prefix="s" uri="/struts-tags"%>
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
    <body>
        <h1>Enum Test</h1>
        //NOTE THE USE OF THE $ character to access the inner class on the following two lines.
        length: <s:property value="@com.action.test.EnumTest$Numbers@values().length"/><br/>
        <s:iterator value="@com.action.test.EnumTest$Numbers@values()">
            <s:property/><br/>
        </s:iterator> 
    </body>
</html>

输出


枚举测试

长度: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.

<constant name="struts.ognl.allowStaticMethodAccess" value="true"/>

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:

package com.action.test;
import com.opensymphony.xwork2.ActionSupport;

public class EnumTest extends ActionSupport{
    enum Numbers{ONE, TWO, THREE};
}

Example JSP:

<%@taglib prefix="s" uri="/struts-tags"%>
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
    <body>
        <h1>Enum Test</h1>
        //NOTE THE USE OF THE $ character to access the inner class on the following two lines.
        length: <s:property value="@com.action.test.EnumTest$Numbers@values().length"/><br/>
        <s:iterator value="@com.action.test.EnumTest$Numbers@values()">
            <s:property/><br/>
        </s:iterator> 
    </body>
</html>

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.

<constant name="struts.ognl.allowStaticMethodAccess" value="true"/>
泅渡 2024-11-30 21:08:08

有点像。您不能直接迭代枚举,因为它不是值的集合(枚举引用仅代表枚举常量之一)。但是,您可以迭代枚举(它是一个数组)的 values() 方法,或者您可以在操作中创建一个 EnumSet 并迭代它。

枚举示例

package example;

public enum SomeEnum {
  ONE, TWO, THREE;

  /* I don't recall if/how you can refer to non-getters in OGNL. */
  public String getName() {
    return name();
  }
}

JSP 示例

<s:iterator value="@example.SomeEnum@values()">
  <s:property value="name"/>
</s:iterator>

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 an EnumSet in your action and iterate that.

Example Enum

package example;

public enum SomeEnum {
  ONE, TWO, THREE;

  /* I don't recall if/how you can refer to non-getters in OGNL. */
  public String getName() {
    return name();
  }
}

Example JSP

<s:iterator value="@example.SomeEnum@values()">
  <s:property value="name"/>
</s:iterator>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文