OGNL允许执行方法,但是默认情况下禁用静态访??问,因此您不能在表达式中使用静态方法。但是,您可以教OGNL哪些类需要访问静态方法。
ognl/developer-guide.html#Method_Accessors)
方法调用是OGNL需要基于动态信息对方法进行查找的另一个领域。该MethodAccessor
接口提供了有关OGNL如何调用方法的信息。当请求静态或实例方法时,将调用此接口的实现者以实际执行该方法。
public interface MethodAccessor
{
Object callStaticMethod( Map context, Class targetClass, String
methodName, List args ) throws MethodFailedException;
Object callMethod( Map context, Object target, String methodName,
List args ) throws MethodFailedException;
}
您可以使用逐个类地设置方法访问器OgnlRuntime.setMethodAccessor()
。是Object的默认方法访问器(它仅根据方法名称和参数类型查找适当的方法,并使用反射来调用该方法)。
您可以编写一些东西
public class StringUtil extends StringUtils implements MethodAccessor {
//implement above methods
}
public static final String MESSAGE = "hello.message";
/**
* Field for Message property.
*/
private String message;
/**
* Return Message property.
*
* @return Message property
*/
public String getMessage() {
return message;
}
private StringUtil stringUtil = new StringUtil();
public StringUtil getStringUtil() {
return stringUtil;
}
public String execute() throws Exception {
setMessage(getText(MESSAGE));
OgnlRuntime.setMethodAccessor(StringUtil.class, stringUtil);
return SUCCESS;
}
<s:if test="!stringUtil.isEmpty(message)">
<h2><s:property value="message"/></h2>
</s:if>