Struts 2在validation器扩展中使用StringUtils
我们使用Struts 2validation器@FieldExpressionValidator
和@ExpressionValidator
。 这些validation器检查OGNL表达式。 在很多情况下,我们在这些表达式中处理字符串。
expression="(captcha=='' && captcha== null || ....)
我们发现如果我们可以在这里使用StringUtils(isEmpty,trimToEmpty,…)非常有用。
当我们将struts.ognl.allowStaticMethodAccess
设置为false时,出于安全问题,我们尝试通过将此getter添加到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,就必须允许静态方法访问或为方法编写包装器,即
public boolean stringUtilsIsEmpty(String captcha) { return StringUtils.isEmpty(captcha); }
然后
ActionContext.getContext().getValueStack().findValue("stringUtilsIsEmpty('dd')");
但是,在JSP中你可以做到
do something
这与StringUtils#isEmpty()
。