velocity学习笔记

源文件地址:
Home Java 网络编程
Velocity笔记
周五, 2010年 01月 15日 09:09

“Velocity将Java代码从Web 页面中分离出来,使用Web站点从长远看更容易维护,并且提供了一种可行的JavaServer Pages替代解决方案。”——Velocity用户手册

Velocity下载地址:http://velocity.apache.org/

在开发中,我们必须导入\lib\log4j-1.2.12.jar和velocity-1.6-dep.jar两个包。如果使用velocity-1.6.jar会稍微麻烦一点,需要把\lib下的commons-collections-3.2.1.jar\commons-lang-2.4.jar和oro-2.0.8.jar放入类路径下。 velocity-1.6-dep.jar文件内部已经包含前面三个jar文件的类。

导入开发包以后,我们需要在类路径中加入velocity.properties文档:

指定日志文件存放位置

runtime.log = E:\\spring\\velocity\\velocity_example.log

指定模版文件加载位置

file.resource.loader.path=E:\\spring\\velocity

指定输入编码格式

input.encoding=UTF-8

指定velocity的servlet向浏览器输出内容的编码

default.contentType=text/html; charset\=UTF-8

指定输出编码格式

output.encoding=UTF-8

接下来是老黎讲的10个例子:

一:

try{

Velocity.init("src/velocity.properties");

VelocityContext context = new VelocityContext();

context.put("hello", "HelloWorld");

context.put("who", "黎明");

Template template = Velocity.getTemplate("mytemplate.vm");

StringWriter sw = new StringWriter();

template.merge(context, sw);

sw.flush();

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

}catch( Exception e ){

e.printStackTrace();

}

mytemplate.vm内容如下:

${who}说:${hello}

二:访问对象

try{

Velocity.init("src/velocity.properties");

VelocityContext context = new VelocityContext();

context.put("person", new Person(2,"小张"));

Template template = Velocity.getTemplate("mytemplate.vm");

StringWriter sw = new StringWriter();

template.merge(context, sw);

sw.flush();

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

}catch( Exception e ){

e.printStackTrace();

}

mytemplate.vm内容如下:

${person.id}=${person.name}

三:历遍集合/数组

try{

Velocity.init("src/velocity.properties");

VelocityContext context = new VelocityContext();

context.put("list", Arrays.asList("第一个","第二个","第三个"));

Template template = Velocity.getTemplate("mytemplate.vm");

StringWriter sw = new StringWriter();

template.merge(context, sw);

sw.flush();

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

}catch( Exception e ){

e.printStackTrace();

}

mytemplate.vm内容如下:

#foreach($element in $list)

$element

#end

四:历遍Map集合

try{

Velocity.init("src/velocity.properties");

VelocityContext context = new VelocityContext();

Map<String,String> map = new HashMap<String, String>();

map.put("key1", "value1");

map.put("key2", "value2");

context.put("map", map);

Template template = Velocity.getTemplate("mytemplate.vm");

StringWriter sw = new StringWriter();

template.merge(context, sw);

sw.flush();

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

}catch( Exception e ){

e.printStackTrace();

}

mytemplate.vm内容如下:

#foreach( $key in $map.keySet() )

$key=$map.get($key)

#end

五:获得当前迭代的索引

try{

Velocity.init("src/velocity.properties");

VelocityContext context = new VelocityContext();

Map<String,String> map = new HashMap<String, String>();

map.put("key1", "value1");

map.put("key2", "value2");

context.put("map", map);

Template template = Velocity.getTemplate("mytemplate.vm");

StringWriter sw = new StringWriter();

template.merge(context, sw);

sw.flush();

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

}catch( Exception e ){

e.printStackTrace();

}

mytemplate.vm内容如下:

#foreach( $key in $map.keySet() )

$velocityCount > $key=$map.get($key)

#end

velocityCount 变量名可以通过directive.foreach.counter.name属性修改,如: directive.foreach.counter.name =index,以后可以通过$index进行访问。迭代的索引默认从1开始,我们可以通过directive.foreach.counter.initial.value=0进行修改。

六:在模版中进行赋值

#set( $name = "老张" )

$name


#set( $condition = true )


数组赋值:

#set( $ints = [1..10])

#foreach( $entry in $ints)

$entry

#end


#set( $ints = ["第一个","第二个"])

#foreach( $entry in $ints)

$entry

#end

七:#if#else#end

try{

Velocity.init("src/velocity.properties");

VelocityContext context = new VelocityContext();

context.put("condition", true);

Template template = Velocity.getTemplate("mytemplate.vm");

StringWriter sw = new StringWriter();

template.merge(context, sw);

sw.flush();

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

}catch( Exception e ){

e.printStackTrace();

}

mytemplate.vm内容如下:

#if($condition)

成立

#else

不成立

#end

八:格式化日期

格式化日期必须将velocity-tools-generic-1.4.jar包加入到类路径。

try{

Velocity.init("src/velocity.properties");

VelocityContext context = new VelocityContext();

context.put("now", new Date());

context.put("dateformat", new DateTool());

Template template = Velocity.getTemplate("mytemplate.vm");

StringWriter sw = new StringWriter();

template.merge(context, sw);

sw.flush();

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

}catch( Exception e ){

e.printStackTrace();

}

mytemplate.vm内容如下:

$dateformat.format("yyyy-MM-dd", $now)

九:输出到文件

try{

Velocity.init("src/velocity.properties");

VelocityContext context = new VelocityContext();

context.put("title", "巴巴运行网");

context.put("body", "这是内容");

Template template = Velocity.getTemplate("mytemplate.vm");

File savefile = new File("E:\\spring\\velocity\\WebRoot\\page\\test.html");

if(!savefile.getParentFile().exists()) savefile.getParentFile().mkdirs();

FileOutputStream outstream = new FileOutputStream(savefile);

OutputStreamWriter writer = new OutputStreamWriter(outstream,"UTF-8");

BufferedWriter bufferWriter = new BufferedWriter(writer);

template.merge(context, bufferWriter);

bufferWriter.flush();

outstream.close();

bufferWriter.close();

}catch( Exception e ){

e.printStackTrace();

}

十:null处理

try{

Velocity.init("src/velocity.properties");

VelocityContext context = new VelocityContext();

context.put("title", null);

Template template = Velocity.getTemplate("mytemplate.vm");

StringWriter writer = new StringWriter();

BufferedWriter bufferWriter = new BufferedWriter(writer);

template.merge(context, bufferWriter);

bufferWriter.flush();

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

}catch( Exception e ){

e.printStackTrace();

}

mytemplate.vm内容如下:

$title
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值