java - Struts 2 在 validator 表达式中使用 StringUtils

标签 java struts2 ognl valuestack

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

相关文章:

java - URLClassLoader 不工作

java - 如何使用 Box Java SDK 从图像文件中获取特定大小的缩略图?

java - Oracle 存储过程在 Linux 上运行时失败,在 Windows 上工作

java - 在scriptlet数组索引中访问struts迭代器的索引值

java - 如何在 JSP 中获取 Action 名称,即使它是空的?

java - 是否可以在 "Function.apply"方法中获取List中当前项的索引?

jquery - SSL AJAX jQuery

java - 如何将复选框的多个值以param=value1%2Cvalue2格式发送到后端?

jquery - 为什么 jQuery 函数要向请求添加额外的参数?

java - 使用迭代器时如何获取 Struts 2 表单的所有键