Java Struts 2在验证器验证中使用StringUtils

Java Struts 2在验证器验证中使用StringUtils,java,struts2,ognl,valuestack,Java,Struts2,Ognl,Valuestack,我们正在使用Struts 2验证程序@FieldExpressionValidator和@ExpressionValidator。这些验证器检查OGNL表达式。在很多情况下,我们在这些表达式中处理字符串 expression="(captcha=='' && captcha== null || ....) 我们发现在这里使用StringUtils(isEmpty、trimToEmpty等)非常有用 当我们将struts.ognl.allowStaticMethodAccess设

我们正在使用Struts 2验证程序
@FieldExpressionValidator
@ExpressionValidator
。这些验证器检查OGNL表达式。在很多情况下,我们在这些表达式中处理字符串

expression="(captcha=='' && captcha== null || ....)
我们发现在这里使用StringUtils(isEmpty、trimToEmpty等)非常有用

当我们将
struts.ognl.allowStaticMethodAccess
设置为false时,为了安全问题,我们试图通过将此getter添加到操作中来解决它

public StringUtils getStringUtils(){
        return new StringUtils();
    }
然后在表达式中输入
stringUtils.isEmpty(验证码)
。但它不起作用

为了调试,我们测试了

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);
}
然后

然而,在JSP中,您可以


做点什么

这与
StringUtils#isEmpty()

一样,因此我应该为每个
StringUtil
s方法添加一个方法:-(有更好的解决方案吗?可能有很多方法。我不知道你具体在问什么,在你的问题中有效的代码在这里,不起作用的代码在这里。澄清你的具体问题,或者它看起来太宽泛了。或者你只是在找注释?亲爱的@RomanC是的,代码有效谢谢,我在找一个
enhancement
一种无需为每个
StringUtils
方法分别定义每个方法的方法。如果你认为这是另一个问题,我可以用另一个问题问它!如果你澄清了你实际需要什么,你可以问另一个问题。
ActionContext.getContext().getValueStack().findValue("stringUtilsIsEmpty('dd')");