Velocity是一个基于java的模板引擎。它允许任何人仅仅简单的使用模板语言来引用由java代码
定义的对象。
工程目录为:
[img]http://dl2.iteye.com/upload/attachment/0086/1051/6a60e33f-f675-3c1e-a93c-8d0cb8c1f2fc.jpg[/img]
example.vm内容如下:
Example.java代码如下:
运行结果如下:
定义的对象。
工程目录为:
[img]http://dl2.iteye.com/upload/attachment/0086/1051/6a60e33f-f675-3c1e-a93c-8d0cb8c1f2fc.jpg[/img]
example.vm内容如下:
#set( $this = "Velocity")
$this is great!
#foreach( $name in $list )
$name is great!
#end
#set( $condition = true)
#if ($condition)
The condition is true!
#else
The condition is false!
#end
Example.java代码如下:
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package com.test;
import java.io.BufferedWriter;
import java.io.OutputStreamWriter;
import java.util.ArrayList;
import java.util.Properties;
import org.apache.velocity.Template;
import org.apache.velocity.VelocityContext;
import org.apache.velocity.app.Velocity;
import org.apache.velocity.exception.ParseErrorException;
import org.apache.velocity.exception.ResourceNotFoundException;
public class Example
{
public Example(String templateFile)
{
try
{
Properties p = new Properties() ;
String basePath = "src/template";
p.setProperty(Velocity.FILE_RESOURCE_LOADER_PATH, basePath);
Velocity.init(p);
VelocityContext context = new VelocityContext();
context.put("list", getNames());
Template template = null;
try
{
template = Velocity.getTemplate(templateFile);
}
catch( ResourceNotFoundException rnfe )
{
System.out.println("Example : error : cannot find template " + templateFile );
}
catch( ParseErrorException pee )
{
System.out.println("Example : Syntax error in template " + templateFile + ":" + pee );
}
BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(System.out));
if ( template != null)
template.merge(context, writer);
writer.flush();
writer.close();
}
catch( Exception e )
{
System.out.println(e);
}
}
@SuppressWarnings("unchecked")
public ArrayList getNames()
{
ArrayList list = new ArrayList();
list.add("ArrayList element 1");
list.add("ArrayList element 2");
list.add("ArrayList element 3");
list.add("ArrayList element 4");
return list;
}
public static void main(String[] args)
{
new Example("example.vm");
}
}
运行结果如下:
Velocity is great!
ArrayList element 1 is great!
ArrayList element 2 is great!
ArrayList element 3 is great!
ArrayList element 4 is great!
The condition is true!