Velocity代碼示例

        String userDir = System.getProperty("user.dir");
		Properties p = new Properties();
		p.setProperty(VelocityEngine.FILE_RESOURCE_LOADER_PATH, userDir + "/file");
		Velocity.init(p);
		
		Template t = Velocity.getTemplate("mytest.vm");
		
		VelocityContext ctx = new VelocityContext();

		ctx.put("name", "velocity");
		ctx.put("date", (new Date()).toString());

		List temp = new ArrayList();
		temp.add("1");
		temp.add("2");
		ctx.put("list", temp);

		StringWriter sw = new StringWriter();

		t.merge(ctx, sw);

		System.out.println(sw.toString());

mytest.vm

#parse("./mytest1.vm")

#parse("./mytest2.vm")

mytest1.vm

#set( $iAmVariable = "good!" )
Welcome $name to velocity.com
today is $date.
$iAmVariable

mytest2.vm

#foreach ($i in $list)
$i
#end


--result

Welcome velocity to velocity.com
today is Wed Sep 23 21:42:08 CST 2015.
good!1
2

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章