模板引擎 Thymeleaf 动态渲染 HTML
2020-05-31 18:24
567 查看
1、添加依赖
<!-- Thymeleaf 模板引擎 --> <dependency> <groupId>org.thymeleaf</groupId> <artifactId>thymeleaf</artifactId> <version>3.0.9.RELEASE</version> </dependency>
2、编码工具类 HTMLTemplateUtils.java
import org.thymeleaf.TemplateEngine; import org.thymeleaf.context.Context; import java.util.Map; /** * HTML模板渲染工具类 */ public class HTMLTemplateUtils { private final static TemplateEngine templateEngine = new TemplateEngine(); /** * 使用 Thymeleaf 渲染 HTML * @param template HTML模板 * @param params 参数 * @return 渲染后的HTML */ public static String render(String template, Map<String, Object> params){ Context context = new Context(); context.setVariables(params); return templateEngine.process(template, context); } }
3、测试模板引擎
import com.odianyun.util.sensi.HTMLTemplateUtils; import org.junit.Test; import java.util.HashMap; import java.util.Map; public class TemplateResolverAttributesTest { @Test public void testTemplateResolutionAttributes01() throws Exception { String template = "<p th:text='${title}'></p>"; Map<String, Object> params = new HashMap<>(); params.put("title", "Thymeleaf 渲染 HTML ---- Anoy"); String output = HTMLTemplateUtils.render(template, params); System.out.println(output); } }
控制台输出
<p>Thymeleaf 渲染 HTML ---- Anoy</p>
相关文档
本文由博客一文多发平台 OpenWrite 发布!
相关文章推荐
- 模板引擎 Thymeleaf 动态渲染 HTML
- UIWebView---iOS中使用模板引擎渲染HTML界面
- 发布一款原创JavaScript模板引擎插件(将html模板快速渲染成数据对应的HTML片段的工具)
- Thymeleaf:面向Java的XML/HTML模板引擎
- 架构小白到砖家-06-【界面渲染问题】-整合模板引擎thymeleaf
- Thymeleaf:面向Java的XML/HTML模板引擎
- springboot通过thymeleaf模板实现动态html模板转pdf文件
- Spring Boot☞ 使用Thymeleaf模板引擎渲染web视图
- express框架的ejs模板引擎渲染html页面
- Spring Boot学习笔记7:使用Thymeleaf模板引擎渲染web视图
- HTML模板渲染引擎Hogan
- 简单的html渲染模板引擎
- Spring Boot 与 kotlin 使用Thymeleaf模板引擎渲染web视图
- 【Web】artTemplate模板引擎:编译(生成渲染函数)+渲染(生成HTML串) 模板:基于JS的语法(JS+自定义)+HTML
- UIWebView---iOS中使用模板引擎渲染HTML界面
- html模板渲染引擎有什么作用
- springboot使用thymeleaf模板渲染html,报错“whitelabel error page,type=not found,status=404”
- SpringBoot当中如何整合动态html模板:Thymeleaf
- Spring Boot 与 kotlin 使用Thymeleaf模板引擎渲染web视图的方法
- Thymeleaf:面向Java的XML/HTML模板引擎