关于Java的Velocity模板使用 1 Velocity

这是我参与11月更文挑战的第15天,活动详情查看:11月更文挑战

Velocity是一个基于java的模板引擎,它允许任何人仅仅使用简单的模板语言来引用由java代码定义的对象。

1 Velocity的简介

Velocity模板引擎, 作为一款成熟的基于java的模板引擎,能够帮我们实现页面静态化,同时它将Java代码与网页分开,将模板和填入数据整合,生成我们需要的页面.

1 基本语法

1 关键字

Velocity模板中的关键字, 都是以#开头表示的

  • #set 设置一个变量
  • #if 条件分支判断
  • #else 另一个条件分支
  • #end 语句结束
  • #foreach 循环语句

2 变量

Velocity模板中的变量, 都是以$开头表示的

如: $user用户 $password 用户密码

{}变量

对于明确的Velocity变量, 可以使用{}包括起来, 可以在页面上展示如下效果:

${user}Name, 此时页面上可以表示为$someoneName的效果.

!变量

如上述内容,Velocity模板中如果变量不存在, 在页面会显示$user, 这种形式影响展示的效果. 可以使用$!user表示.

$!user表示, 存在则展示,不存在则为空白

1
2
3
4
5
6
7
8
9
10
11
12
vm复制代码## 定义一个user变量为李白, password变量为123456 
#set{$user = "李白"}
#set{$password = "123456"}

## 变量引用
#set{$student.name = "李白"}
## 数字
#set{$student.age = 22}
## 字符串
#set{$student.class = "大班"}
## 属性引用
#set($student.address = $address.info)

3 转义字符和逻辑操作符

Velocity模板中转义字符是 \

1
2
3
4
5
6
vm复制代码#set{$user = "李白"}
## 输入 结果
$user 李白
\$user $user
\\$user \李白
\\\$user \$user

&& 且

|| 或

! 取反

4 循环

Velocity模板中list集合循环语法

循环遍历,可以得到每个元素,每个元素的序号,以及总的集合长度

1
2
3
4
5
6
7
8
vm复制代码#foreach ( $element in $list)
## 集合中每个元素
$element
## 集合的序号 从1开始
${velocityCount}
## 集合的长度
${list.size()}
#end

map集合循环语法

1
2
3
4
vm复制代码#foreach ($entry in $map.entrySet())
## map的key map的value值
$entry.key => $entry.value
#end

5 条件

Velocity模板中条件语法if-ifelse-else结构

1
2
3
4
5
6
7
vm复制代码#if (condition1)
// 执行业务
#elseif (condition2)
// 执行业务
#else
// 执行业务
#end

常用的条件语句是if-else结构

1
2
3
4
5
vm复制代码#if (condition1)
// 执行业务
#else
// 执行业务
#end

#break

表示跳出循环

1
2
3
4
5
6
7
8
vm复制代码#if (condition1)
## 条件符合跳过
#if($user == "李白")
#break;
#end
#else
// 执行业务
#end

#stop

表示终止指令,终止模板解析

1
2
3
4
5
6
7
8
vm复制代码#if (condition1)
## 条件符合直接终止
#if($user == "李白")
#stop
#end
#else
// 执行业务
#end

6 注释

单行注释 ##

1
2
vm复制代码## 定义一个user变量为李白
#set{$user = "李白"}

多行注释 #* *#

1
2
3
4
5
vm复制代码#*  
定义一个user变量
将user变量赋值为 李白
*#
#set{$user = "李白"}

文档注释 #** *#

1
2
3
4
5
vm复制代码 #** 
@version 1.1
@author 李白
*#
#set{$user = "李白"}

7 引入资源

#include

表示引入外部资源,引入的资源不被引擎所解析

1
vm复制代码#include( "one.gif","two.txt","three.htm" )

#parse

用于导入脚本, 引入的资源会被引擎所解析

1
2
3
4
5
6
7
8
vm复制代码##  a.vm文件
#set($user = "李白")


## b.vm文件
#parse("a.vm")
## 变量 值
$user 李白

2 Velocity的使用

Velocity常用的案例和工具类

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
java复制代码public class VelocityUtils {

public static void main(String[] args) {

// 模板路径
String templatePath = "D:\\work";
// 模板名称
String templateName = "index.html.vm";
// 生成文件路径
String outFilePath = "D:\\index.html";
// 模板中所需参数
Map<String, Object> params = new HashMap<>();
params.put("name", "world");
List<String> list = new ArrayList<>();
list.add("李白");
list.add("杜甫");
list.add("陆游");
params.put("list", list);

getFile(templatePath,templateName,outFilePath,params);
}

/**
* 读取本地模板,生成文件
* @param templatePath 模板路径
* @param templateName 模板名称
* @param outFilePath 生成文件路径
* @param params 模板中填充参数
*/
public static void getFile(String templatePath, String templateName, String outFilePath,
Map<String, Object> params) {
try {
// 创建属性
loadTemplateFileByTwo(templatePath);

// 封装填充参数
VelocityContext context = new VelocityContext(params);
// 获取模板
Template tpl = Velocity.getTemplate(templateName, "UTF-8");
// 创建输出流
Writer writer = new PrintWriter(new FileOutputStream(new File(outFilePath)));
// 模板与数据填充
tpl.merge(context, writer);
// 刷新数据
writer.flush();
writer.close();
} catch (IOException e) {
e.printStackTrace();
}
}

/**
* 送请求, 将生成的模板文件使用zip压缩包返回
* @param response 响应对象
* @param params 模板封装参数
* @throws IOException
*/
public static void createFile(HttpServletResponse response, Map<String, Object> params)
throws IOException {

ByteArrayOutputStream output = new ByteArrayOutputStream();
ZipOutputStream outZip = new ZipOutputStream(output);

// 设置velocity资源加载器
loadTemplateFileByOne();

// 封装模板数据
VelocityContext context = new VelocityContext(params);

//获取模板列表
List<String> templates = getTemplates();
for (String template : templates) {
// 渲染模板
StringWriter sw = new StringWriter();
Template tpl = Velocity.getTemplate(template, "UTF-8");
tpl.merge(context, sw);

// 添加数据
outZip.putNextEntry(new ZipEntry(getFileName(template)));
IOUtils.write(sw.toString(), outZip, "UTF-8");
IOUtils.closeQuietly(sw);
}

IOUtils.closeQuietly(outZip);
byte[] data = output.toByteArray();

// 生成zip压缩包响应
response.setHeader("Content-Disposition", "attachment; filename=\"template-file.zip\"");
response.addHeader("Content-Length", String.valueOf(data.length));
response.setContentType("application/octet-stream; charset=UTF-8");
IOUtils.write(data, response.getOutputStream());

}

/**
* 获取文件名
*/
/**
* @param template 模板名 如index.html.vm
*/
private static String getFileName(String template) {
return template.replace(".vm", "");
}

/**
* 获取模板
*/
private static List<String> getTemplates() {
List<String> templates = Lists.newArrayList();
// 后端相关模板
templates.add("index.html.vm");
return templates;
}


/**
* 加载配置文件方法一 加载classpath目录下的vm文件
*/
public static void loadTemplateFileByOne() {
Properties p = new Properties();
p.put("file.resource.loader.class",
"org.apache.velocity.runtime.resource.loader.ClasspathResourceLoader");
Velocity.init(p);
}

/**
* 加载配置文件方法二 加载绝对路径目录下的加载vm文件
*
* @param templatePath 模板路径
*/
public static void loadTemplateFileByTwo(String templatePath) {
Properties p = new Properties();
// 设置模板加载路径 为D盘 work文件夹
p.setProperty(VelocityEngine.FILE_RESOURCE_LOADER_PATH, templatePath);
Velocity.init(p);
}

/**
* 加载配置文件方法三 使用配置文件
*
* @param propertiesPath ,如:/velocity.properties
*/
public static void loadTemplateFileByThree(String propertiesPath) throws IOException {
Properties p = new Properties();
p.load(VelocityUtils.class.getClass().getResourceAsStream(propertiesPath));
Velocity.init(p);
}

}

其中index.html.vm模板文件在D:盘work文件夹中

1
2
3
4
5
6
7
8
9
10
11
12
13
14
html复制代码<table cellspacing="0" cellpadding="5" width="20%" >
<tr>
<td bgcolor="#eeeeee" align="center">
Names:${name}
</td>
</tr>
#foreach($name in $list)
<tr>
<td>
第${velocityCount}个, 名字为 $name , 总共 ${list.size()} 个
</td>
</tr>
#end
</table>

生成的index.html文件

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
html复制代码<table cellspacing="0" cellpadding="5" width="20%" >
<tr>
<td bgcolor="#eeeeee" align="center">
Names:world
</td>
</tr>
<tr>
<td>
第1个, 名字为 李白 , 总共 3 个
</td>
</tr>
<tr>
<td>
第2个, 名字为 杜甫 , 总共 3 个
</td>
</tr>
<tr>
<td>
第3个, 名字为 陆游 , 总共 3 个
</td>
</tr>
</table>

从上面测试的案例,可知,name参数有了, list集合参数有. 对于一些日常常规的循环条件判断等, Velocity模板引擎非常好用.

参考资料:

www.51gjie.com/javaweb/896…

yanglinwei.blog.csdn.net/article/det…

本文转载自: 掘金

开发者博客 – 和开发相关的 这里全都有

0%