Search code examples
struts2static-methodsognl

Struts 2 refactoring code to avoid OGNL static method access


Struts 2, 2.3.20 mentioned that

Support for accessing static methods from expression will be disabled soon, please consider re-factoring your application to avoid further problems!

We have used OGNL static calls in validators:

@ExpressionValidator(
 expression = "@foo.bar@isValidAmount(amount)",
 key = "validate.amount.is.not.valid"),

Also we used it in tags

<s:set var="test"
value="@foo.bar@sampleMethod(#attr.sampleObject.property1)" />

Well, what is the best way to refactor above two usages ?!


Solution

  • In your code you are using a static method call. The best way is to create a method in the action class that wraps a static methods and use it in OGNL.

    public class Wrapper {
      public boolean isValidAmount(amount){
         return foo.barr.isValidAmount(amount);
      }
      public Object sampleMethod(Object property1){
         return foo.barr.sampleMethod(Object property1);
      }
    
    }
    

    As soon as action bean is in the value stack you can use

    @ExpressionValidator(
     expression = "isValidAmount(amount)",
     key = "validate.amount.is.not.valid"),
    

    or in JSP

    <s:set var="test"
    value="sampleMethod(#attr.sampleObject.property1)" />