首页  

模板引擎velocity实例     所属分类 velocity 浏览量 831
模板引擎velocity 



Properties properties = new Properties();
properties.setProperty("resource.loader", "class");
properties.setProperty("class.resource.loader.class",
    "org.apache.velocity.runtime.resource.loader.ClasspathResourceLoader");
VelocityEngine velocityEngine = new VelocityEngine(properties);
        
VelocityContext context = new VelocityContext();
context.put("name", "VELOCITY");
        
String[] strList = {"abc","xyz"};
context.put("strList", strList);
        
List<Map<String,Object>> userList = new ArrayList<>();
Map<String,Object> map = new HashMap<>();
map.put("id", 1);
map.put("name", "dyyx");
userList.add(map);
        
map = new HashMap<>();
map.put("id", 2);
map.put("name", "tiger");
userList.add(map);
context.put("userList", userList);
        
StringWriter writer = new StringWriter();
velocityEngine.mergeTemplate("template/hello.vm", "utf8", context, writer);
System.out.println(writer);
        
       


模板文件 src/main/resources/template/hello.vm #set( $name2 = "velocity") hello , ${name2} <br> hello , $name <br> #foreach( $item in $strList) $item <br> #end #foreach( $item in $userList) $item.id $item.name <br> #end 完整代码 https://gitee.com/dyyx/demos/blob/master/templatedemo/src/main/java/dyyx/VelocityDemo.java

上一篇     下一篇
JDK CGLIB Javassist ASM 动态代理 比较

SpringBoot异步任务 @Async

groovy实例

freemarker实例

dubbo消费端独立启动不依赖spring

项目经理要做的事情