【问题标题】:Accessing static variable using OGNL in Struts2在 Struts2 中使用 OGNL 访问静态变量
【发布时间】:2011-08-02 02:17:49
【问题描述】:

美好的一天!

我正在阅读 Manning 的 struts2 书,其中一个主题是使用 OGNL 使用语法 @[fullClassName]@[property or methodCall] 访问静态变量

所以我在我的程序上试了一下,我的代码如下:

豆:

public class ContactsBean {

    private static int count = 1;
    //getter and setter
}

动作:

private ContactsBean contacts;
//getters and setters

JSP:

   <s:property value="@com.demo.bean.ContactsBean@count" />

or
    <s:property value="@vs@count" />  //valuestack method

但它不起作用。我错过了什么吗? 谢谢。

【问题讨论】:

    标签: jakarta-ee struts2 ognl


    【解决方案1】:

    @see OGNL Basics : Accessing static properties

    public class ContactsBean {
        private static int count = 1; 
    
        // static getter
        // setter
    }
    

    <s:property value="@com.demo.bean.ContactsBean@getCount()" />
    

    其他情况

    public class ContactsBean {
        public static final int SCALE = 50;
    }
    

    <s:property value="@com.demo.bean.ContactsBean@SCALE" />
    

    【讨论】:

    【解决方案2】:

    Apache Struts 2 文档 - struts.properties http://struts.apache.org/2.0.14/docs/strutsproperties.html

    要启用静态方法访问/调用,请在基础包的 struts.properties 文件中设置 Struts2 常量:

    struts.ognl.allowStaticMethodAccess=true 
    

    ..或者我相信你可以在struts.xml中设置为

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

    【讨论】:

      【解决方案3】:

      如果我们在 struts.xml 中提到以下条目,它就可以正常工作

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

      【讨论】:

        【解决方案4】:

        正如新版本的 struts 2 (2.3.20) 中所述,这个 (struts.ognl.allowStaticMethodAccess) 将很快从 struts 中删除。

        请查看Struts 2 refactoring code to avoid OGNL static method access,了解如何在新版本中仍然使用此功能。

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2017-11-01
          • 1970-01-01
          • 1970-01-01
          • 2010-10-19
          • 2015-08-05
          相关资源
          最近更新 更多