Struts 2在验证器扩展中使用StringUtils [英] Struts 2 using StringUtils in validator expersions
问题描述
我们正在使用Struts 2验证器 @FieldExpressionValidator
和 @ExpressionValidator
。这些验证器检查OGNL表达式。在很多情况下,我们会在这些表达式中处理字符串。
We are using Struts 2 validators @FieldExpressionValidator
and @ExpressionValidator
. These validators check on OGNL expression. There are lots of cases where we deal with Strings in these expressions.
expression="(captcha=='' && captcha== null || ....)
我们发现它非常有用在这里使用StringUtils(isEmpty,trimToEmpty,...)。
We find it is very useful if we can use StringUtils ( isEmpty ,trimToEmpty,... ) here.
我们设置 struts.ognl.allowStaticMethodAccess
为了安全问题,我们尝试通过将此getter添加到操作来解决此问题
As we set the struts.ognl.allowStaticMethodAccess
to false, for security issues, we tried to solve it by adding this getter to action
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,就必须允许静态方法访问或为方法编写包装器,即
isEmpty
is a static method and should be accessed statically with class prefix. As soon as you are using OGNL you have to allow static method access or write a wrapper for the method, i.e.
public boolean stringUtilsIsEmpty(String captcha) {
return StringUtils.isEmpty(captcha);
}
然后
ActionContext.getContext().getValueStack().findValue("stringUtilsIsEmpty('dd')");
然而,在JSP中你可以做到
However, in JSP you can do
<s:if test="captcha != null && captcha != ''">
do something
</s:if>
这与 StringUtils#isEmpty()$ c相同$ c>。
这篇关于Struts 2在验证器扩展中使用StringUtils的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!