本文主要是介绍Thymeleaf:生成静态文件及异常处理java.lang.NoClassDefFoundError: ognl/PropertyAccessor,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
我们需要引入包:
<dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
<dependency><groupId>org.springframework</groupId><artifactId>spring-context-support</artifactId>
</dependency>
<dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-test</artifactId><scope>test</scope>
</dependency>
<dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency><groupId>ognl</groupId><artifactId>ognl</artifactId><version>3.2.10</version><scope>test</scope>
</dependency>
我们首先创建Thymeleaf模版:index.html
<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org"><head><meta charset="UTF-8"><title>Title</title></head><body><h1 th:text="${title}">Hello World</h1></body>
</html>
然后我们再编写输出静态网页的方法:TstToHtml
package com.kingbal.tohtml.test;import org.thymeleaf.TemplateEngine;
import org.thymeleaf.context.Context;
import org.thymeleaf.templatemode.TemplateMode;
import org.thymeleaf.templateresolver.ClassLoaderTemplateResolver;import java.io.File;
import java.io.FileWriter;
import java.io.IOException;/*** <b>Function: </b> todo** @program: TstToHtml* @Package: com.kingbal.tohtml.test* @author: songjianlin* @date: 2024/09/06* @version: 1.0* @Copyright: 2024 www.kingbal.net Inc. All rights reserved.*/
public class TstToHtml {public static void main(String[] args) throws IOException {ClassLoaderTemplateResolver resolver = new ClassLoaderTemplateResolver();resolver.setPrefix("templates/"); //模板文件的所在目录resolver.setSuffix(".html"); //模板文件的后缀resolver.setTemplateMode(TemplateMode.HTML);//创建模板引擎对象TemplateEngine templateEngine = new TemplateEngine();//将加载器放入模板引擎对象中templateEngine.setTemplateResolver(resolver);String path = "D:\\git\\html";File file=new File(path);if(!file.exists()){file.mkdir();}FileWriter writer = new FileWriter(file.getPath() + "\\index.html");//创建Context对象(存放Model)Context context = new Context();context.setVariable("title", "测试列表");templateEngine.process("index", context, writer);}
}
当我们运行的时候出现错误,找了很多资料,说什么版本的问题,其实都试了没用
Exception in thread "main" java.lang.NoClassDefFoundError: ognl/PropertyAccessorat org.thymeleaf.standard.StandardDialect.getVariableExpressionEvaluator(StandardDialect.java:178)at org.thymeleaf.standard.StandardDialect.getExecutionAttributes(StandardDialect.java:392)at org.thymeleaf.DialectSetConfiguration.build(DialectSetConfiguration.java:263)at org.thymeleaf.EngineConfiguration.<init>(EngineConfiguration.java:123)at org.thymeleaf.TemplateEngine.initialize(TemplateEngine.java:341)at org.thymeleaf.TemplateEngine.process(TemplateEngine.java:1084)at org.thymeleaf.TemplateEngine.process(TemplateEngine.java:1072)at com.kingbal.tohtml.test.TstToHtml.main(TstToHtml.java:50)
Caused by: java.lang.ClassNotFoundException: ognl.PropertyAccessorat java.base/jdk.internal.loader.BuiltinClassLoader.loadClass(BuiltinClassLoader.java:641)at java.base/jdk.internal.loader.ClassLoaders$AppClassLoader.loadClass(ClassLoaders.java:188)at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:525)... 8 more
解决这个问题主要是在 TemplateEngine 实例化的时候需要基于spring,所以在创建模版引擎的时候实例化 SpringTemplateEngine 即可
//创建模板引擎对象
TemplateEngine templateEngine = new SpringTemplateEngine();
这样就可以成功生成静态网页了~~~
这篇关于Thymeleaf:生成静态文件及异常处理java.lang.NoClassDefFoundError: ognl/PropertyAccessor的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!