java - 进行 Thymeleaf/OGNL 方法访问时出现 IllegalAccessException

标签 java google-app-engine thymeleaf ognl

我是网络开发新手,我正在 App Engine 上尝试 Thymeleaf 模板引擎。到目前为止,它工作得很好,除了当我尝试方法(而不是简单的属性)访问时,我总是得到 java IllegalAccessException。

例如,使用这行 HTML:

<div class="panel" th:id="${item.getWebId()}">

我得到:

java.lang.IllegalAccessException:
Method [public java.lang.String myapp.ItemInfo.getWebId()] cannot be accessed.
at ognl.OgnlRuntime.invokeMethod(OgnlRuntime.java:851) 

这种情况会发生在开发服务器 (Win Vista) 上以及使用 GAE SDK 1.9.0 和 1.8.9 的生产环境中。我已经尝试过当前的 TL (2.1.2) 和以前的版本 (2.0.20)。我没有使用 Spring。

我在 TL 论坛上发现其他人遇到反射问题( herehere ),但没有任何帮助。

很早以前就有人使用Struts或者Spring遇到过类似的问题,通过设置OgnlRuntime.setSecurityManager(null)来解决。我没有追求这个,因为我看不到如何访问 TL 中的 OgnlRuntime 对象,而且我是唯一需要这个的 TL 用户,这对我来说没有多大意义。

查看OgnlRuntime v3.0.6 (here)看起来最简单的解决方案是禁用“checkPermission”,但同样,作为 TL 的新手,我犹豫是否要进行 TL 未公开的更改,并且其他使用 TL 的人似乎不需要这样做。一定还有什么问题吗?

最佳答案

最后我继续使用 OgnlRuntime.setSecurityManager(null) 选项。这似乎是一个坏主意,但它允许我现在继续。

这是我使用的代码:

public class MyContextListener implements ServletContextListener {
    @Override
    public void contextDestroyed(ServletContextEvent arg0) {
    }
    @Override
    public void contextInitialized(ServletContextEvent arg0) {
        OgnlRuntime.setSecurityManager(null);
    }
}

在我的 web.xml 中我添加了:

<listener>
    <listener-class>com.myapp.MyContextListener</listener-class>
</listener>

关于java - 进行 Thymeleaf/OGNL 方法访问时出现 IllegalAccessException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22314530/

相关文章:

java - 使用 Camel 发送 ActiveMQ 消息时出现 ClassCastException

google-app-engine - 在基于 Android-Studio 的项目中为 AppEngine 创建综合索引

google-app-engine - 为什么我的谷歌应用引擎有域名 'my-project.df.r.appspot.com' ?

java - 在 thymeleaf 中使用 db 列作为 img src

thymeleaf - 使用 thymeleaf 直接渲染 View 片段

java - 是否可以像比较数字一样比较字符串?

java - 更改默认 Maven 路径?

python - Linux CI 服务器上的 GAE

html - thymeleaf 中的for循环

java - 两个数组或一个 HashMap 将字符串转换为摩尔斯电码?