Velocity是一个基于java的模板引擎(template engine)。它允许任何人仅仅简单的使用模板语言(template language)来引用由java代码定义的对象。
Java工程中引入Velocity的包, 这里是velocity-1.4.jar, velocity-dep-1.4.jar.
TestVelocity.java:
package
tony.test;
import java.io.StringWriter;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Properties;
import org.apache.velocity.VelocityContext;
import org.apache.velocity.app.Velocity;
import org.apache.velocity.app.VelocityEngine;
import org.apache.velocity.context.Context;
import org.apache.velocity.exception.MethodInvocationException;
import org.apache.velocity.exception.ParseErrorException;
import org.apache.velocity.exception.ResourceNotFoundException;
import org.apache.velocity.runtime.RuntimeConstants;
/** */ /**
* @author Tony
*/
public class TestVelocity
... {
private static VelocityEngine engine = null;
/** *//**
* Initialize velocity
*/
public static void initVelocity() throws Exception
...{
engine = new VelocityEngine();
Properties prop = new Properties();
// Set the template file path
prop.setProperty(Velocity.FILE_RESOURCE_LOADER_PATH, "E:/templates");
engine.init(prop);
}
/** *//**
* @param templeteFileName: Velocity templete file name.
* @param values: Velocity templete variables and values map.
* @return Generated content string.
*/
private static String generateContent(String templeteFileName, Map values) throws ResourceNotFoundException, ParseErrorException, MethodInvocationException, Exception
...{
Context context = new VelocityContext();
for (Iterator it=values.entrySet().iterator(); it.hasNext();)
...{
Map.Entry entry = (Map.Entry)it.next();
context.put((String)entry.getKey(), entry.getValue());
}
StringWriter wr = new StringWriter();
engine.mergeTemplate(templeteFileName, RuntimeConstants.ENCODING_DEFAULT, context, wr);
return wr.toString();
}
public static void main(String[] args) throws Exception
...{
initVelocity();
List dothings = new ArrayList();
dothings.add(new DoThings("Haha", "Giggle"));
dothings.add(new DoThings("Hehe", "Smile"));
dothings.add(new DoThings("Kaka", "Die"));
Map values = new HashMap();
values.put("YOUR_CHARACTERISTIC","old");
values.put("YOUR_NAME","Tony");
values.put("DO_THINGS", "laugh");
values.put("REPEAT_TIME", new Integer(dothings.size()));
values.put("ALL_TIMES", dothings);
System.out.println(generateContent("velocity.helloworld.vm", values));
}
}
import java.io.StringWriter;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Properties;
import org.apache.velocity.VelocityContext;
import org.apache.velocity.app.Velocity;
import org.apache.velocity.app.VelocityEngine;
import org.apache.velocity.context.Context;
import org.apache.velocity.exception.MethodInvocationException;
import org.apache.velocity.exception.ParseErrorException;
import org.apache.velocity.exception.ResourceNotFoundException;
import org.apache.velocity.runtime.RuntimeConstants;
/** */ /**
* @author Tony
*/
public class TestVelocity
... {
private static VelocityEngine engine = null;
/** *//**
* Initialize velocity
*/
public static void initVelocity() throws Exception
...{
engine = new VelocityEngine();
Properties prop = new Properties();
// Set the template file path
prop.setProperty(Velocity.FILE_RESOURCE_LOADER_PATH, "E:/templates");
engine.init(prop);
}
/** *//**
* @param templeteFileName: Velocity templete file name.
* @param values: Velocity templete variables and values map.
* @return Generated content string.
*/
private static String generateContent(String templeteFileName, Map values) throws ResourceNotFoundException, ParseErrorException, MethodInvocationException, Exception
...{
Context context = new VelocityContext();
for (Iterator it=values.entrySet().iterator(); it.hasNext();)
...{
Map.Entry entry = (Map.Entry)it.next();
context.put((String)entry.getKey(), entry.getValue());
}
StringWriter wr = new StringWriter();
engine.mergeTemplate(templeteFileName, RuntimeConstants.ENCODING_DEFAULT, context, wr);
return wr.toString();
}
public static void main(String[] args) throws Exception
...{
initVelocity();
List dothings = new ArrayList();
dothings.add(new DoThings("Haha", "Giggle"));
dothings.add(new DoThings("Hehe", "Smile"));
dothings.add(new DoThings("Kaka", "Die"));
Map values = new HashMap();
values.put("YOUR_CHARACTERISTIC","old");
values.put("YOUR_NAME","Tony");
values.put("DO_THINGS", "laugh");
values.put("REPEAT_TIME", new Integer(dothings.size()));
values.put("ALL_TIMES", dothings);
System.out.println(generateContent("velocity.helloworld.vm", values));
}
}
DoThings.java:
package
tony.test;
/** */ /**
* @author Tony
* The class should be public, and implement get/set.
*/
public class DoThings
... {
private String name;
private String things;
public DoThings(String name, String things)
...{
this.name = name;
this.things = things;
}
public String getName()
...{
return name;
}
public void setName(String name)
...{
this.name = name;
}
public String getThings()
...{
return things;
}
public void setThings(String things)
...{
this.things = things;
}
}
/** */ /**
* @author Tony
* The class should be public, and implement get/set.
*/
public class DoThings
... {
private String name;
private String things;
public DoThings(String name, String things)
...{
this.name = name;
this.things = things;
}
public String getName()
...{
return name;
}
public void setName(String name)
...{
this.name = name;
}
public String getThings()
...{
return things;
}
public void setThings(String things)
...{
this.things = things;
}
}
E:/templates/velocity.helloworld.vm(完整路径):
<
html
>
< body >
I am the $YOUR_CHARACTERISTIC $YOUR_NAME.
< br />< br />
I will $DO_THINGS for $REPEAT_TIME times:
< br />< br />
#foreach($EACH_TIME in $ALL_TIMES)
$velocityCount. $EACH_TIME.name -- $EACH_TIME.things
< br />
#end
< br />
#if($YOUR_NAME == "Tony")
Best wishes!
#else
Go to die!
#end
</ body >
</ html >
< body >
I am the $YOUR_CHARACTERISTIC $YOUR_NAME.
< br />< br />
I will $DO_THINGS for $REPEAT_TIME times:
< br />< br />
#foreach($EACH_TIME in $ALL_TIMES)
$velocityCount. $EACH_TIME.name -- $EACH_TIME.things
< br />
#end
< br />
#if($YOUR_NAME == "Tony")
Best wishes!
#else
Go to die!
#end
</ body >
</ html >
生成结果:
<
html
>
< body >
I am the old Tony.
< br />< br />
I will laugh for 3 times:
< br />< br />
1. Haha -- Giggle
< br />
2. Hehe -- Smile
< br />
3. Kaka -- Die
< br />
< br />
Best wishes!
</ body >
</ html >
< body >
I am the old Tony.
< br />< br />
I will laugh for 3 times:
< br />< br />
1. Haha -- Giggle
< br />
2. Hehe -- Smile
< br />
3. Kaka -- Die
< br />
< br />
Best wishes!
</ body >
</ html >
一份不错的参考:
Velocity用户手册---中文版(http://www.ijsp.net/2/2003-9/27/0000434.shtml).