1

我们正在使用 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(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

任何意见 ?!

4

1 回答 1

1

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()

于 2016-10-05T11:45:25.387 回答