velocity需要在lib下面导入4个文件包,如下面jpg图片所示:
velocity-1.7.zip的文件包在下面也给了下载地址
------------HelloVelocity .java--------------------
import java.io.StringWriter;
import java.util.ArrayList;
import java.util.Date;
import java.util.Hashtable;
import java.util.List;
import org.apache.velocity.Template;
import org.apache.velocity.VelocityContext;
import org.apache.velocity.app.VelocityEngine;
public class HelloVelocity {
public static void main(String[] args) throws Exception {
//初始化并取得Velocity引擎
VelocityEngine ve = new VelocityEngine();
ve.init();
//取得velocity的模版
Template t = ve.getTemplate("src/hellovelocity.vm"); //在src路劲下面放着的时间
//取得velocity的上下文context
VelocityContext context = new VelocityContext();
//把数据填入上下文
context.put("name","Liang");
context.put("bing","lujiebing");
context.put("date", (new Date()).toString());
//为后面的展示,提前输入List数值
List temp = new ArrayList();
temp.add("1");
temp.add("2");
context.put("list", temp);
//hashtable
Hashtable table=new Hashtable();
table.put("a", 1);
table.put("b", 1);
table.put("c", 1);
context.put("table", table);
//javabean
context.put("person",new PersonBean("xuli","1234"));
//输出流
StringWriter writer = new StringWriter();
//转换输出
t.merge(context, writer);
System.out.println(writer.toString());
}
}
---------helloVelocity.vm---------------------
Welcome $name to Javayou.com!
today is $date.
#set($bing = "lujie")
welcome $bing to see you!
#foreach( $product in $list )
<$velocityCount> $product
#end
#foreach( $key in $table.keySet() )
$key value: $table.get($key)
#end
Address: $person.name
- 大小: 20.6 KB
分享到:
相关推荐
Velocity入门教程,语法,Velocity布局,Spring框架集成Velocity
包括以下四个基本文档: Velocity初级入门指南.doc velocity基础.doc Velocity教程.pptx velocity入门使用教程.doc
Velocity入门教程实例,包括velocity介绍,语法详解,以及具体的示例!
Velocity入门Demo,含源代码, 直接放到tomcat可以运行
Velocity入门,velocity是基于Java的引擎模版
Velocity模板入门简单DEMO, 代码有注解;
入门文档及应用源码,很适合做自动代码生成 包括:Velocity的中文指南\ velocity中文手册\ \基于Ant+Velocity的简单代码生成器的思路与实现
初学velocity,做的一个小实例。
velocity的简单入门使用,主要适合刚开始学习velocity的人使用,也可以作为平时的参考
velocity入门简介,可作为入门学习,培训材料1
从Velocity的基础说起,讲到了其语法、然后结合其他流行技术进行延伸讲解,希望对你有帮助——资料仅有前14章,但已足够学习了
Apache-Velocity-java
velocity快速入门
NULL 博文链接:https://yaphis.iteye.com/blog/2080752
Velocity模板引擎Velocity模板引擎Velocity模板引擎Velocity模板引擎Velocity模板引擎Velocity模板引擎Velocity模板引擎Velocity模板引擎Velocity模板引擎Velocity模板引擎
文档是velocity基本知识,语法运用,宏定义等,适合新手入门
velocity入门实例,java代码读取配置文件 可直接运行
l 快速入门 l vtl 模板语法 l 代码生成器案例 第二部分 : velocity tools 高级进阶 l velocity tools 介绍 l GenericTools使用 l Velocity View 使用 l 自定义tools工具类 l springmvc整合velocity l 实现对用户...