非Spring环境下Thymeleaf运行网页遇NoClassDefFoundError问题求助
解决Thymeleaf与OGNL类初始化错误问题
问题根源
你遇到的java.lang.NoClassDefFoundError: Could not initialize class org.thymeleaf.standard.expression.OGNLVariableExpressionEvaluator错误,是因为手动添加的OGNL版本与Thymeleaf 3.0.15.RELEASE不兼容,且缺少Thymeleaf标准表达式方言的核心依赖,导致OGNL相关类无法正确初始化。
修复步骤
1. 调整Maven依赖
修改pom.xml,移除单独的OGNL依赖,添加Thymeleaf标准方言依赖(该依赖会自动引入匹配版本的OGNL,避免版本冲突),同时给Servlet API添加provided scope避免服务器端依赖冲突:
<dependencies> <!-- 保留其他原有依赖,调整以下部分 --> <dependency> <groupId>org.thymeleaf</groupId> <artifactId>thymeleaf</artifactId> <version>3.0.15.RELEASE</version> </dependency> <!-- 添加Thymeleaf标准方言依赖 --> <dependency> <groupId>org.thymeleaf</groupId> <artifactId>thymeleaf-standard-dialect</artifactId> <version>3.0.15.RELEASE</version> </dependency> <!-- 移除单独的OGNL依赖 --> <!-- <dependency> <groupId>ognl</groupId> <artifactId>ognl</artifactId> <version>3.1.26</version> </dependency> --> <!-- 标记Servlet API为服务器提供,避免打包冲突 --> <dependency> <groupId>javax.servlet</groupId> <artifactId>javax.servlet-api</artifactId> <version>4.0.1</version> <scope>provided</scope> </dependency> </dependencies>
2. 清理并重新构建项目
执行Maven命令完成依赖更新:
mvn clean install
3. 重启服务器
确保服务器加载了新的依赖包,之后重新访问/genreOverview页面即可正常渲染内容。
额外说明
你的Thymeleaf配置、Servlet代码和HTML模板本身无逻辑问题,修复依赖后应该能正常展示Hello World、模块信息及城市列表内容。
内容的提问来源于stack exchange,提问作者kledek
相关产品推荐
相关产品推荐

