<!-- Thymeleaf 模板引擎 -->
<dependency>
<groupId>org.thymeleaf</groupId>
<artifactId>thymeleaf</artifactId>
<version>3.0.9.RELEASE</version>
</dependency>复制代码
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);
}
}复制代码
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);
}
}复制代码
控制台输出
java
Thymeleaf 渲染 HTML ---- Anoy
面试
Thymeleaf 模板语法spring
© 著做权归做者全部,转载或内容合做请联系做者框架
● spring-boot-starter-grpc 不一样序列化方式性能测试及选型spring-boot
● Spring Boot 2 集成log4j2日志框架工具
● Spring Security 实战干货:如何保护用户密码编码
● Spring Boot RabbitMQ - 优先级队列spa
本文由博客一文多发平台 OpenWrite 发布!