- package org.demo;
- import java.io.StringWriter;
- import java.util.ArrayList;
- import java.util.Date;
- import java.util.HashMap;
- import java.util.List;
- import java.util.Map;
- import org.apache.velocity.Template;
- import org.apache.velocity.VelocityContext;
- import org.apache.velocity.app.Velocity;
- import org.apache.velocity.app.VelocityEngine;
- import org.demo.bean.Student;
- public class Test {
- public static void main(String[] args) throws Exception {
- /* first, get and initialize an engine */
- VelocityEngine ve = new VelocityEngine();
- // 设置类路径加载模板
- ve.setProperty(Velocity.RESOURCE_LOADER, "class");
- ve.setProperty("class.resource.loader.class",
- "org.apache.velocity.runtime.resource.loader.ClasspathResourceLoader");
- ve.init();
- /* next, get the Template */
- Template t = ve.getTemplate("org/demo/hello.vm");
- // Example 1
- /* create a context and add data */
- VelocityContext context = new VelocityContext();
- context.put("name", "sunny");
- context.put("site", "http://cutesunshineriver.iteye.com");
- context.put("date", (new Date()).toString());
- // Example 2
- /* fill the context with a list */
- List<String> temp = new ArrayList<String>();
- temp.add("1");
- temp.add("2");
- context.put("list", temp);
- // Example 3
- /* fill the context with hashmap */
- Map<String, Integer> map = new HashMap<String, Integer>();
- map.put("key1", 3);
- map.put("key2", 4);
- context.put("map", map);
- // Example 4
- /* fill the context with javabean list */
- List<Student> students = new ArrayList<Student>();
- students.add(new Student(5, "broadway 26"));
- students.add(new Student(6, "sunset avenue 26"));
- context.put("students", students);
- /* now render the template into a StringWriter */
- StringWriter writer = new StringWriter();
- t.merge(context, writer);
- /* show the World */
- System.out.println(writer.toString());
- }
- }
2、vm文件
- #set($aVar = "good!")
- Hello $name! Welcome to $site world!
- today is $date.
- $aVar
- ====================================
- Logic below:
- #set($admin = "admin")
- #set($user = "user")
- #if ($admin == $user)
- Welcome admin!
- #else
- Welcome user!
- #end
- ====================================
- List below:
- #foreach( $product in $list )
- $product
- #end
- ====================================
- Map below:
- #foreach($key in $map.keySet())
- $key's value: $map.get($key)
- #end
- ====================================
- student beans below:
- #foreach ($s in $students)
- <$velocityCount> No: $s.no; Address: $s.address
- #end
- ====================================
- #macro ( tablerows $color $somelist )
- #foreach ( $something in $somelist )
- <tr><td bgcolor=$color>$something</td</tr>
- #end
- #end
- #set ( $greatlakes = [ "Superior", "Michigan", "Huron", "Erie", "Ontario" ] )
- #set ( $color = "blue" )
- <table>
- #tablerows( $color $greatlakes )
- </table>
- ====================================
- #foreach ( $bar in [2..-2] )
- $bar
- #end
3、输出
Hello sunny! Welcome to http://cutesunshineriver.iteye.com world!
today is Sun Apr 10 09:09:12 CST 2011.
good!
====================================
Logic below:
Welcome user!
====================================
List below:
1
2
====================================
Map below:
key2's value: 4
key1's value: 3
====================================
student beans below:
<1> No: 5; Address: broadway 26
<2> No: 6; Address: sunset avenue 26
====================================
<table>
<tr><td bgcolor=blue>Superior</td</tr>
<tr><td bgcolor=blue>Michigan</td</tr>
<tr><td bgcolor=blue>Huron</td</tr>
<tr><td bgcolor=blue>Erie</td</tr>
<tr><td bgcolor=blue>Ontario</td</tr>
</table>
====================================
2
1
0
-1
-2