Velocity demo
2016-03-10 16:05
134 查看
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文件
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
相关文章推荐
- java clinit 死锁
- java.lang.NoSuchMethodError: javax.xml.ws.WebFault.messageName()Ljava/lang/Strin
- EA强大的画图工具---设计数据库表格
- 实时流计算Spark Streaming原理介绍
- Redis 发布订阅
- FastDFS、Nginx安装、配置、测试(java client)
- IKAnalyzer如何自定义远端词库
- JVM系列三:JVM参数设置、分析
- Velocity语法大全
- MyBatis的几种批量操作
- 转:Java中用HttpsURLConnection访问Https链接的问题
- redis持久化RDB和AOF
- nexus搭建和手动更新索引心得
- 分布式与集群
- java 多线程下载图片
- 淘宝SOA框架dubbo学习--异步调用
- Spark Streaming实时计算框架介绍
- JVM调优:GC 参数
- IP地址漂移技术
- Velocity浅析及与Jsp、Freemarker对比