Struts 2 refactoring code to avoid OGNL static method access

末鹿安然 提交于 2019-11-28 05:34:59

问题


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 ?!


回答1:


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


来源:https://stackoverflow.com/questions/28018861/struts-2-refactoring-code-to-avoid-ognl-static-method-access

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!