maven
<dependency>
<groupId>org.apache.velocity</groupId>
<artifactId>velocity-engine-core</artifactId>
<version>2.3</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.13.1</version>
<scope>test</scope>
</dependency>
Test方法
@Test
public void test() throws IOException {
Properties prop = new Properties();
// 设置资源加载器
prop.put("file.resource.loader.class","org.apache.velocity.runtime.resource.loader.ClasspathResourceLoader");
// 初始化volocity引擎
Velocity.init(prop);
//创建velocity模板
VelocityContext vc = new VelocityContext();
// 也可以直接放对象
Map<String,Object> map = new HashMap<>();
map.put("username","lisi");
map.put("password","22222");
// map.put("age",1);
vc.put("user", map);
vc.put("name","zhangsan");
vc.put("content","ni shi big boy");
vc.put("date",new Date());
vc.put("list", Arrays.asList("1","2","3"));
Map<String,Object> cat1 = new HashMap<>();
cat1.put("name","花狸");
cat1.put("action","pu");
Map<String,Object> cat2 = new HashMap<>();
cat2.put("name","无毛");
cat2.put("action","卖萌");
vc.put("catList", Arrays.asList(cat1,cat2));
vc.put("lll","#if($lang.equals('cn')) 没错了 #end");
Template template = Velocity.getTemplate("vms/htm1.vm", "utf-8");
try (FileWriter fw = new FileWriter("D:\\jd_project\\Velocity\\src\\main\\resources\\html\\htm1.html")) {
template.merge(vc,fw);
}
}
vm文件
- htm1.vm
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
## 单行注释
#*
多行注释
*#
#**
* 文档注释
*#
## 不解析符号
#[[
${name}
]]#
## 没有数据时候 直接展示符号
${age}
$age
## 没有数据时候 直接展示""
$!{age}
$!age
你好 ${name} 来了啊
### 对象展示
$user.username -- $user.password -- $user.age
${user.username} -- ${user.password} -- ${user.age}
$!user.username -- $!user.password -- $!user.age
$!{user.username} -- $!{user.password} -- $!{user.age}
## 方法
$content.split(" ") --- $list.size() ---- $date.getTime()
${content.split(" ")} --- ${list.size()} ---- ${date.getTime()}
$!content1.split(" ") --- $!list1.size() ---- $!date1.getTime()
$!{content1.split(" ")} --- $!{list1.size()} ---- $!{date1.getTime()}
## 指令
#set($int=1)
#set($str="nihaoa")
#set($map={"key":"value"})
#set($list=[1,2,3,4])
$int
$str
$map.get("key")
$list
## if指令
#set($list=[1,2])
#if($list.size()==1)
大哥 数据就一条
#elseif($list.size()==2)
二哥 我们是兄弟
#elseif($list.size()==3)
三哥 我们是铁三角
#else
啥玩意不懂
#end
## 遍历
#set($list=[1,2])
#foreach($num in $list)
$foreach.index --- $foreach.count --- $num
#end
<hr>
#foreach($cat in $catList)
$foreach.index --- $foreach.count --- $cat.name ---- $cat.action
#end
<hr/>
#set($map={"key":"value","key1":"value1","key3":"value3"})
#foreach($entry in $map.keySet())
键:$entry 值 : $map.get($entry)
#end
#foreach($entry in $map.entrySet())
键:$entry.key 值 : $entry.value
#end
## include 不会处理数据 直接拿过来
#include("vms/body.vm")
## parse 会解析
#parse("vms/body.vm")
##define() 定义模块 会解析
#define($table)
<table>
<tr>
<td> 哈哈</td>
</tr>
<tr>
<td> 嘿嘿</td>
</tr>
<tr>
<td> 喵喵</td>
</tr>
<tr>
<td> ${name}</td>
</tr>
</table>
#end
define
$table
## evaluate vc.put("lll","#if($lang.equals('cn')) 没错了 #end");
#set($lang="cn")
#evaluate($lll)
## macro() 第一个参数 是模块名称 第二个时传入的参数
#macro(tab $list)
<table>
<tr>
<td> 姓名</td>
<td> 动作</td>
</tr>
#foreach($cat in $list)
<tr>
<td> $cat.name</td>
<td> $cat.action</td>
</tr>
#end
</table>
#end
## 这块调用需要用#符号
#tab($catList)
</body>
</html>
- body.vm
<div>
include -- $!{name}
</div>
<div>
parse -- $!{name}
</div>