spring boot 中使用 thymeleaf 报错ognl/PropertyAccessor

文章介绍了当遇到`java.lang.NoClassDefFoundError:ognl/PropertyAccessor`错误时的解决方法,该错误是由于Thymeleaf在pom文件中排除了ognl依赖导致的。解决方案是在项目pom文件中添加ognl依赖,版本为3.1.26。同时,文章提供了出现错误的代码示例和正常Thymeleaf依赖的结构对比。
摘要由CSDN通过智能技术生成

错误提示

java.lang.NoClassDefFoundError: ognl/PropertyAccessor

解决方法

在 pom 文件中添加如下依赖:

<dependency>
    <groupId>ognl</groupId>
    <artifactId>ognl</artifactId>
    <version>3.1.26</version>
</dependency>

错误重现

出现错误的代码:

public class Tests {
    @Test
    public void test0() {
        TemplateEngine templateEngine = new TemplateEngine();

        String input = "<p th:text='${age}'></p>";

        Context context = new Context();
        context.setVariable("age", "3");

        String output = templateEngine.process(input, context);
        System.out.println(output);
    }
}

pom 文件:

<dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-thymeleaf</artifactId>
    </dependency>
    <dependency>
        <groupId>org.junit.jupiter</groupId>
        <artifactId>junit-jupiter-api</artifactId>
        <scope>test</scope>
    </dependency>
</dependencies>

错误原因

在 org.thymeleaf:thymeleaf-spring5 的 pom 文件中,排除了 ognl:

<dependency>
    <groupId>org.thymeleaf</groupId>
    <artifactId>thymeleaf</artifactId>
    <version>3.0.15.RELEASE</version>
    <scope>compile</scope>
    <exclusions>
        <exclusion>
            <groupId>ognl</groupId>
            <artifactId>ognl</artifactId>
        </exclusion>
    </exclusions>
</dependency>

依赖关系如下图:

对比一下正常的 org.thymeleaf:thymeleaf 的依赖关系:

  • 2
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值