在发送邮件时需要直接获取thymeleaf渲染后的html字符串,实现方法如下:
基础条件:项目中引入thymeleaf依赖
src\main\resources\templates\test.html
<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8"/>
<title>spring boot-thymeleaf test</title>
</head>
<body>
<p th:text="'hello, ' + ${name} + '!'"></p>
</body>
</html>
测试:
@SpringBootTest
class JzApplicationTests {
@Autowired
private TemplateEngine templateEngine;
@Test
void thymeleaf() {
Context context = new Context();
context.setVariable("name", "xxxxxxxxx");
String result = templateEngine.process("test", context);
System.out.println(result);
}
}
网友评论