Struts 2 重构代码以避免 OGNL 静态方法访问 [英] Struts 2 refactoring code to avoid OGNL static method access
问题描述
Struts 2, 2.3.20 提到
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!
我们在验证器中使用了 OGNL 静态调用:
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 ?!
推荐答案
在您的代码中,您使用的是静态方法调用.最好的方法是在 action 类中创建一个封装静态方法的方法并在 OGNL 中使用.
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);
}
}
只要 action bean 在值堆栈中,您就可以使用
As soon as action bean is in the value stack you can use
@ExpressionValidator(
expression = "isValidAmount(amount)",
key = "validate.amount.is.not.valid"),
或在 JSP 中
<s:set var="test"
value="sampleMethod(#attr.sampleObject.property1)" />
这篇关于Struts 2 重构代码以避免 OGNL 静态方法访问的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!