我们正在使用 Struts 2 validator @FieldExpressionValidator
和 @ExpressionValidator
。这些 validator 检查 OGNL 表达式。在很多情况下,我们都会在这些表达式中处理字符串。
expression="(captcha=='' && captcha== null || ....)
我们发现如果我们可以在这里使用 StringUtils ( isEmpty ,trimToEmpty,... ) 会非常有用。
当我们将 struts.ognl.allowStaticMethodAccess
设置为 false 时,出于安全问题,我们尝试通过添加此 getter 来解决它
public StringUtils getStringUtils(){
return new StringUtils();
}
然后在表达式中使用stringUtils.isEmpty(captcha)
。但没有成功。
为了调试,我们进行了测试
ActionContext.getContext().getValueStack().findValue("stringUtils"); //returns org.apache.commons.lang3.StringUtils@693ade51 which shows there is an object in the stack
ActionContext.getContext().getValueStack().findValue("stringUtils.isEmpty('dd')"); //returns null
有什么意见吗?!
最佳答案
isEmpty
是一个静态方法,应该使用类前缀静态访问。一旦您使用 OGNL,您就必须允许静态方法访问或为该方法编写包装器,即
public boolean stringUtilsIsEmpty(String captcha) {
return StringUtils.isEmpty(captcha);
}
然后
ActionContext.getContext().getValueStack().findValue("stringUtilsIsEmpty('dd')");
但是,在 JSP 中你可以这样做
<s:if test="captcha != null && captcha != ''">
do something
</s:if>
这与StringUtils#isEmpty()
的作用相同。
关于java - Struts 2 在 validator 表达式中使用 StringUtils,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39870217/