资料引入依赖定义模板使用附
1. 工程结构示例2. 执行结果示例3. 示例源码
资料官网
引入依赖定义模板org.thymeleaf thymeleaf3.1.0.M1
以html为例
Title
使用
以下测试代码分别演示了输出到变量和流的情况,更多用法可以参考官网或源代码
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;
import java.io.Writer;
public class ThymeleafDemo {
public static void main(String[] args) throws IOException {
// 定义、设置模板解析器
ClassLoaderTemplateResolver templateResolver = new ClassLoaderTemplateResolver();
// 设置模板类型 # https://www.thymeleaf.org/doc/tutorials/3.0/usingthymeleaf.html#what-is-thymeleaf
// HTML、XML、TEXT、JAVAscript、CSS、RAW
templateResolver.setTemplateMode(TemplateMode.HTML);
templateResolver.setPrefix("/template/");
templateResolver.setSuffix(".html");
// 定义模板引擎
TemplateEngine templateEngine = new TemplateEngine();
templateEngine.setTemplateResolver(templateResolver);
// 定义数据模型
Context context = new Context();
context.setVariable("username","张三");
// 渲染模板 (输出到变量(控制台))
System.out.println(templateEngine.process("test", context));
// 输出到流(文件)
File file = new File("E:\","test_out.html");
Writer write = new FileWriter(file);
templateEngine.process("test",context,write);
}
}
附
1. 工程结构示例
2. 执行结果示例
https://gitee.com/master336/template-engine-demo


![[模板引擎] thymeleaf 独立环境使用 [模板引擎] thymeleaf 独立环境使用](http://www.mshxw.com/aiimages/31/721181.png)
