Struts 2迭代枚举 [英] Struts 2 iterate enum

查看:81
本文介绍了Struts 2迭代枚举的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Struts 2是否可以使用标签< s:iterator> 迭代枚举?
现在我正在使用String的列表,但是可以直接使用枚举吗?

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?

提前感谢。

推荐答案

是的。这是一个有点丑陋的答案是启用静态方法访问,使用内部类语法为OGNL表达式(使用'$'),两者将结合将让您得到已经提到的史蒂文的值方法。以下是一个示例:

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:

示例操作

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号

ONE < >

TWO


THREE



注意:确保静态方法访问被启用。简单的方法是在struts.xml中的< struts> 标签之后添加以下内容。

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"/>

这篇关于Struts 2迭代枚举的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆