Struts2 OGNL Calling Common Static Methods Details

  • 2021-11-13 17:52:18
  • OfStack

Struts2 OGNL calls public static methods

1 It has been known for a long time that OGNL of Struts2 supports calling static methods in the background in the foreground. I've used it before, But it is used less, Today, I started a new project again. When I realized a function, I used the static method of calling the background in the foreground of Struts2. After a long time, I didn't come out with the result. It turned out that I didn't declare that I could use the static method in the foreground in the configuration file of Struts, so I wrote it for my reference in the future and took it as a lesson.

Suppose you have an com. xxx. util. Util class with a public static method called sayHello:


public static String sayHello(String name) {
 return "HELLO " + name;
 }

Then the static method can be called in the foreground as follows:


<:property value="@com.xxx.util.Util@sayHello('tiantian')"/>

In order to make it possible to invoke the public static method in the background smoothly in the foreground, it is also necessary to declare in the configuration file of Struts that the subsequent static method can be invoked in the foreground


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

Thank you for reading, hope to help everyone, thank you for your support to this site!


Related articles: