天天看點

spring boot 使用velocity模闆

(不要使用這種模闆了,spring boot最新版已經不支援了。使用FreeMarker吧:http://blog.csdn.net/clementad/article/details/51942629)

簡單幾步,在spring boot中使用velocity模闆生成文本:

1、引入依賴

<dependency>  
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-velocity</artifactId>
		</dependency>
           

2、resources中建立templates目錄

spring boot 使用velocity模闆

3、建立.vm模闆檔案welcome.vm:

<html>
<body>
親愛的${toUserName},你好!

    ${message}

祝:開心!
${fromUserName}
${time}

</body>
</html>
           

4、使用模闆,測試用例:

@Autowired
	VelocityEngine velocityEngine;
	
	@Test
	public void velocityTest(){
		Map<String, Object> model = new HashMap<String, Object>();
		model.put("time", XDateUtils.nowToString());
		model.put("message", "這是測試的内容。。。");
		model.put("toUserName", "張三");
		model.put("fromUserName", "老許");
		System.out.println(VelocityEngineUtils.mergeTemplateIntoString(velocityEngine, "welcome.vm", "UTF-8", model));
	}
           

5、測試結果:

spring boot 使用velocity模闆

附:

velocity官網: http://velocity.apache.org/

velocity文法參考:http://velocity.apache.org/engine/devel/vtl-reference.html

源代碼參考:https://github.com/xujijun/my-spring-boot

繼續閱讀