目录
一、依赖
二、示例代码
一、依赖
<--JAVA 8-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
<version>2.7.18</version>
</dependency>
<--JAVA 17-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
<version>3.1.2</version>
</dependency>
二、示例代码(JAVA 8)
import org.thymeleaf.context.Context;
import org.thymeleaf.spring5.SpringTemplateEngine;
import org.thymeleaf.templateresolver.StringTemplateResolver;
import java.util.HashMap;
import java.util.Map;
public class TestController {
public static void main(String[] args) {
SpringTemplateEngine engine = new SpringTemplateEngine();
StringTemplateResolver resolver = new StringTemplateResolver();
engine.setTemplateResolver(resolver);
Map<String, Object> params = new HashMap<>();
params.put("name", "项羽");
params.put("age", "18");
Context context = new Context();
context.setVariables(params);
String process = engine.process("大家好我的名字是[(${name})],今年[(${age})]岁了", context);
System.out.println(process);
}
}