)创建测试工程,引入依赖
(2)创建模板。在resources目录下创建test.html
<dependency> <groupId>org.thymeleaf</groupId> <artifactId>thymeleaf</artifactId> <version>3.0.11.RELEASE</version> </dependency>
(3)创建测试类,编写代码
1.3.2 常用th标签
<!DOCTYPE html> <html xmlns:th="http://www.thymeleaf.org"> <head> <meta charset="UTF‐8"> <title>thymeleaf入门demo</title> </head> <body> <span th:text="${name}"></span> </body> </html>
// 1.上下文 Context context = new Context(); //创建数据模型 Map<String, Object> dataModel =new HashMap(); dataModel.put("name","青橙电商系统"); context.setVariables(dataModel); // 2.准备文件 File dest = new File("d:/test_out.html"); // 3.生成页面 try { PrintWriter writer = new PrintWriter(dest, "UTF‐8"); ClassLoaderTemplateResolver resolver = new ClassLoaderTemplateResolver();//模板解析器 resolver.setTemplateMode(TemplateMode.HTML);//模板模型 resolver.setSuffix(".html");//后缀
TemplateEngine engine = new TemplateEngine();//创建模板引擎 engine.setTemplateResolver(resolver);//设置模板解析器 engine.process("test", context, writer);//执行模 |
|