9

Good day!

I am reading Manning's struts2 book and one of the topic is accessing the static variable using OGNL using the syntax @[fullClassName]@[property or methodCall]

so I tried it on my program and my code is as follows:

BEAN:

public class ContactsBean {

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

ACTION:

private ContactsBean contacts;
//getters and setters

JSP:

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

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

but it doesn't work. Am i missing something? Thank you.

newbie
  • 14,582
  • 31
  • 104
  • 146

4 Answers4

14

@see OGNL Basics : Accessing static properties

BEAN :

public class ContactsBean {
    private static int count = 1; 

    // static getter
    // setter
}

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

other case

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

<s:property value="@com.demo.bean.ContactsBean@SCALE" />
lschin
  • 6,745
  • 2
  • 38
  • 52
3

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

To enable static method access / invocation set the Struts2 constant in your struts.properties file in your base package:

struts.ognl.allowStaticMethodAccess=true 

.. or I believe you can set it in the struts.xml as

<constant name="struts.ognl.allowStaticMethodAccess" value="true"/> 
Russell Shingleton
  • 3,176
  • 1
  • 21
  • 29
0

As mentioned in new release of struts 2 (2.3.20), this (struts.ognl.allowStaticMethodAccess) will be removed soon from struts.

Please review Struts 2 refactoring code to avoid OGNL static method access to find out how you can still use this feature in new version.

Community
  • 1
  • 1
Alireza Fattahi
  • 42,517
  • 14
  • 123
  • 173
0

Its work fine if we mentioned the below entry in struts.xml

  <constant name="struts.ognl.allowStaticMethodAccess" value="true"/> 
prashant thakre
  • 5,061
  • 3
  • 26
  • 39