1、依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-velocity</artifactId>
</dependency>
2、application.properties
spring.velocity.cache= false
spring.velocity.charset=UTF-8
spring.velocity.check-template-location=true
spring.velocity.content-type=text/html
spring.velocity.enabled=true
spring.velocity.resource-loader-path=/templates
spring.velocity.prefix=/templates/
spring.velocity.suffix=.vm
spring.velocity.toolbox-config-location=/WEB-INF/toolbox.xml
3、IndexController .java
@Controller
public class IndexController {
@Autowired
VelocityEngine velocityEngine;
@RequestMapping("/")
public String index(Map map){
map.put("message", "获取用户信息");
map.put("name", "张三");
map.put("age", "24");
System.out.println(VelocityEngineUtils.mergeTemplateIntoString(velocityEngine, "index.vm","UTF-8", map));
return "index";
}
}
4、index.vm
<!DOCTYPE html>
<html>
<head lang="en">
<meta charset="UTF-8" />
<title>Insert title here</title>
</head>
<body>
${message}
${name}
${age}
</body>
</html>