spring boot集成velocity可以分为如下几个步骤:
引入Velocity包
引入Velocity包,只需要在pom.xml中引入即可,如下所示:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-velocity</artifactId>
</dependency>
创建模板目录文件
创建模板目录文件默认如下所示:
创建模板文件
模板文件如上图所示。注意:默认情况下shareniu.vm文件位于templates目录中。
shareniu.vm文件的内容如下所示:
<html>
<body>
各位领导、同事
大家好!
${message}
${fromUserName}
${time}
</body>
</html>
测试
测试代码如下:
@Autowired
VelocityEngine velocityEngine;
@RequestMapping("/")
String index() {
Map<String, Object> model = new HashMap<String, Object>();
model.put("time", new Date());
model.put("message",
"北京时间2017年5月12日20时左右,全球爆发大规模勒索软件感染事件,我国大量行业企业内网大规模感染,教育网受损严重,攻击造成了教学系统瘫痪,甚至包括校园一卡通系统。建议电脑用户尽快使用360“NSA武器库免疫工具”进行防御。");
model.put("fromUserName", "老许");
System.out.println(VelocityEngineUtils.mergeTemplateIntoString(velocityEngine, "shareniu.vm", "UTF-8", model));
System.out.println(environment);
String property = environment.getProperty("shareniu1");
String c = environment.getProperty("c");
System.out.println("##########################" + property + "####################a:" + c);
return "xxxxxxxxxxxxx";
}
测试结果
<html>
<body>
各位领导、同事
大家好!
北京时间2017年5月12日20时左右,全球爆发大规模勒索软件感染事件,我国大量行业企业内网大规模感染,教育网受损严重,攻击造成了教学系统瘫痪,甚至包括校园一卡通系统。建议电脑用户尽快使用360“NSA武器库免疫工具”进行防御。
老许
Tue May 16 14:34:28 CST 2017
</body>
</html>
转载请注明:分享牛 » spring boot集成velocity