---
title: "在struts2.1.2中不能使用ognl访问静态方法的原因"
author: "hypgr"
date: "2009-04-15T09:52:48Z"
tags: []
slug: "struts2-1-2-ognl"
draft: false
lastmod: "2009-04-15T09:52:48Z"
id: 367982
url: "https://hypgr.iteye.com/blog/367982"
---

&lt;script type="text/javascript"&gt;&lt;/script&gt;

使用struts, 在jsp页面中，可以通过ognl访问类的静态方法。

 

表达式的格式为@\[类全名（包括包路径）]@\[方法名 |  值名]，例如：@java.lang.String@format('foo %s', 'bar') 。页面的输出结果就是：foo bar

 

在目前的项目中，我们使用了struts最新的版本2.1.2,当我在jsp页面中使用ognl访问类的静态方法时，发现不起作用。也没报错信息，找不到原因。网上搜了一下，也未发现相同经历的人。当我把struts的版本降到2.0.11时，发现是可以访问静态方法的，困惑！

 

最后终于发现了原因，原来是在2.1.2中，如果要通过ognl访问静态方法，必须在struts.properties或者struts.xml中将选项**struts.ognl.allowStaticMethodAccess设置为true** .

 

问题解决，记录备忘！