Springboot集成velocity
- 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
- 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
- 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
Springboot集成velocity
1.加⼊maven包
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.4.2.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
2.velocity配置
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 #⽂件名后缀
3.测试页⾯ index.vm
<html>
<body>
亲爱的${toUserName},你好!
${message}
祝:开⼼!
${fromUserName}55
${time}
</body>
</html>
4.后台数据接⼝
@Controller
@SpringBootApplication
public class DemoApplication {
@RequestMapping("/")
public String velocityTest(Map map){
map.put("message", "这是测试的内容。
");
map.put("toUserName", "张三1");
map.put("fromUserName", "⽼许");
return "index";
}
5.springboot启动器
@RunWith(SpringRunner.class)
@SpringBootTest
public class DemoApplicationTests {
@Test
public void contextLoads() {
}
@Autowired
VelocityEngine velocityEngine;
@Test
public void velocityTest(){
Map<String, Object> model = new HashMap<String, Object>();
model.put("message", "这是测试的内容。
");
model.put("toUserName", "张三");
model.put("fromUserName", "⽼许");
System.out.println(VelocityEngineUtils.mergeTemplateIntoString(velocityEngine, "/templates/index.vm", "UTF-8", model)); }
}。