velocity模板发布
2016-07-05 15:21
309 查看
package com.velocitydemo.velocityhandler;
import java.io.StringWriter;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Properties;
import org.apache.velocity.Template;
import org.apache.velocity.VelocityContext;
import org.apache.velocity.app.Velocity;
import org.apache.velocity.app.VelocityEngine;
import org.apache.velocity.runtime.RuntimeConstants;
import org.apache.velocity.runtime.resource.loader.ClasspathResourceLoader;
@SuppressWarnings("unused")
public class HelloVelocity {
public static void main(String[] args) {
VelocityEngine ve = new VelocityEngine();
ve.setProperty(RuntimeConstants.RESOURCE_LOADER, "classpath");
ve.setProperty("classpath.resource.loader.class", ClasspathResourceLoader.class.getName());
ve.init();
Template t = ve.getTemplate("hello.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());
}
}
hello.vm:
效果图:
import java.io.StringWriter;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Properties;
import org.apache.velocity.Template;
import org.apache.velocity.VelocityContext;
import org.apache.velocity.app.Velocity;
import org.apache.velocity.app.VelocityEngine;
import org.apache.velocity.runtime.RuntimeConstants;
import org.apache.velocity.runtime.resource.loader.ClasspathResourceLoader;
@SuppressWarnings("unused")
public class HelloVelocity {
public static void main(String[] args) {
VelocityEngine ve = new VelocityEngine();
ve.setProperty(RuntimeConstants.RESOURCE_LOADER, "classpath");
ve.setProperty("classpath.resource.loader.class", ClasspathResourceLoader.class.getName());
ve.init();
Template t = ve.getTemplate("hello.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());
}
}
hello.vm:
效果图:
相关文章推荐
- html文件中jquery与velocity变量中的$冲突的解决方法
- velocity显示List与Map的方法详细解析
- JAVA velocity模板引擎使用实例
- Velocity基本语法介绍
- Spring MVC 集成 Velocity 配置
- velocity 之坑:不同枚举类(enum)有相同的静态(static)方法,无法访问第二个枚举类
- Velocity入门例子
- Velocity读取后台数据
- 树形结构
- Velocity快速入门教程-脚本语法详解(转)
- velocity 注释语法
- velocity include
- Velocity浅析及与Jsp、Freemarker对比
- Velocity 官方实例学习二 (context_example)
- Velocity 官方实例学习一 (app_example1)
- velocity解析器vm页面Date类型时间显示解决方案
- Velocity入门指南——第一章 快速入门
- Velocity入门指南——第二章 注释
- 一些好用的Eclipse 3.0插件
- 一些好用的Eclipse 3.0插件