栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

当struts.ognl.allowStaticMethodAccess为false时,Struts 2调用静态方法

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

当struts.ognl.allowStaticMethodAccess为false时,Struts 2调用静态方法

OGNL允许执行方法,但是默认情况下禁用静态访​​问,因此您不能在表达式中使用静态方法。但是,您可以教OGNL哪些类需要访问静态方法。

[OGNL开发人员指南:方法访问器](https://commons.apache.org/proper/commons-

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";private String message;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;}

在JSP中

<s:if test="!stringUtil.isEmpty(message)">  <h2><s:property value="message"/></h2></s:if>



转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/438780.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号