Accessing a static variable using OGNL in Struts2

Good day!

I am reading a book by Manning struts2 and one of the topics accesses a static variable using OGNL using the syntax @[fullClassName]@[property or methodCall]

so I tried this in my program and my code looks like this:

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 that will not work. Am I missing something? Thanks.

+8
java-ee struts2 ognl
source share
4 answers

@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()" /> 

another case

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

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

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

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

 struts.ognl.allowStaticMethodAccess=true 

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

 <constant name="struts.ognl.allowStaticMethodAccess" value="true"/> 
+3
source share

His work is great if we mentioned the following entry in struts.xml

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

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

Check out the Struts 2 refactoring code to avoid accessing the static OGNL methods to see how you can still use this feature in the new version.

0
source share

All Articles