当前位置: 代码迷 >> Web前端 >> velocity示范
  详细解决方案

velocity示范

热度:89   发布时间:2012-11-22 00:16:41.0
velocity示例
<html> 
<body> 
#set($foo = "velocity") 
Hello $foo world; 
My name is $myname 
Your name is $yourname 
</body> 
</html> 


public static void main(String[] args) {

		try {
			// 初始化velocity引擎
			VelocityEngine ve = new VelocityEngine();

			Properties pro = new Properties();
			pro.put("file.resource.loader.class",
					"org.apache.velocity.runtime.resource.loader.ClasspathResourceLoader");

			ve.init(pro);

			// 获取模板
			Template template = ve.getTemplate("test/test.vm");

			// 填充数据
			VelocityContext velocityContext = new VelocityContext();
			velocityContext.put("myname", "姚明");
			velocityContext.put("yourname", "麦迪");

			// 合并数据
			StringWriter writer = new StringWriter();
                           //把vm文件中的内容和VelocityContext上下文中的数据合并输出到StringWriter中
			template.merge(velocityContext, writer);
			// 显示
			System.out.println(writer.toString());
		} catch (Exception e) {
			e.printStackTrace();
		}
	}