Apache Velocity 概述
Apache Velocity 是一个基于 Java 的模板引擎,它允许将 Java 代码与 HTML、XML 或其他文本格式分离,实现视图与数据的解耦。在 Web 开发中,Velocity 常用于生成动态网页内容;在其他场景下,也可用于生成配置文件、邮件模板等。
代码示例中的 Velocity 使用说明
1. Java 代码部分 (VelocityDemoTest.java
)
java
package com.dkd.test;
import com.ruoyi.generator.util.VelocityInitializer;
import org.apache.velocity.Template;
import org.apache.velocity.VelocityContext;
import org.apache.velocity.app.Velocity;
import java.io.FileWriter;
import java.util.List;
/**
* @author: jd
* @create: 2025-05-06
*/
public class VelocityDemoTest {
public static void main(String[] args) throws Exception {
//1、初始化模版引擎
VelocityInitializer.initVelocity();
//2、准备模版的数据模型,上下文对象
VelocityContext context = new VelocityContext();
context.put("message", "张三替换");
//创建区域对象
Region region = new Region(1L, "上海");
context.put("region", region);
Region region2 = new Region(2L, "北京");
Region region3 = new Region(3L, "广州");
List<Region> regionList = List.of(region, region2, region3);
context.put("regionList", regionList);
//3、Velocity读取模版文件
Template template = Velocity.getTemplate("vm/index.html.vm", "UTF-8");
//4、Velocity渲染模版,合并输出,创建一个文件输出流
FileWriter fileWriter = new FileWriter("D:\\test\\index.html");
//将变量渲染到模版,并输出到指定的为止的文件中
template.merge(context, fileWriter);
fileWriter.close();//关闭流
}
}
- 初始化模板引擎 :
VelocityInitializer.initVelocity()
:调用自定义的初始化方法对 Velocity 进行必要的配置,例如设置模板文件的加载路径、编码等。
- 准备数据模型 :
VelocityContext context = new VelocityContext()
:创建一个VelocityContext
对象,它是一个数据容器,用于存储要传递给模板的数据。context.put(key, value)
:将数据放入VelocityContext
中,其中key
是在模板中引用该数据的名称,value
是具体的数据对象。在示例中,放入了字符串、自定义对象和对象列表。
- 读取模板文件 :
Velocity.getTemplate("vm/index.html.vm", "UTF-8")
:从指定路径读取模板文件,并指定文件编码为 UTF - 8。
- 渲染模板并输出 :
FileWriter fileWriter = new FileWriter("D:\\test\\index.html")
:创建一个文件输出流,用于将渲染后的内容写入指定文件。template.merge(context, fileWriter)
:将VelocityContext
中的数据与模板文件进行合并,将渲染后的内容输出到文件输出流中。fileWriter.close()
:关闭文件输出流,释放资源。
2. 模板文件部分 (index.html.vm
)
html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewpoint" content="width=device-width,initial-scale=1.0">
<title>Hello World</title>
</head>
<body>
<h5>xxxxxx, ${message}</h5>
## 定义变量,
#set($name = "xxxxxx")
##输出变量
第一种情况:${name} <br>
第二种情况:$name <br>
第三种情况:字符串拼接:${name}Service <br>
<hr>
##获取对象中的属性
$region<br>
区域ID:$region.id<br>
区域名称:$region.regionName<br>
<hr>
##定义一个集合
#set($list = [11,22,33,44,55,66,77,88,99,100])
#foreach($item in $list)
序号[$foreach.count] $item <br> ## count是从1开始的, 索引是从0开始的
索引[$foreach.index] $item <br>
#end
<hr>
#foreach($item in $regionList)
城市ID:$item.id ## count是从1开始的, 索引是从0开始的
城市名称:$item.regionName <br>
#end
#set($score = 95)
#if($score>80)
优秀
#elseif($score>60)
良好
#else
差
#end
</body>
</html>
- 变量定义与输出 :
#set($name = "xxxxxx")
:使用#set
指令定义一个变量$name
,并赋值为"xxxxxx"
。${name}
和$name
:两种方式都可以输出变量的值。在字符串拼接时,建议使用${name}
以避免歧义。
- 对象属性访问 :
$region
:输出对象的字符串表示形式。$region.id
和$region.regionName
:访问对象的属性。需要注意的是,这里的regionName
可能有误,根据 Java 代码中Region
类的定义,应该是name
。
- 循环遍历 :
#foreach($item in $list)
:使用#foreach
指令遍历集合。$foreach.count
表示当前循环的序号(从 1 开始),$foreach.index
表示当前元素的索引(从 0 开始)。- 可以遍历 Java 代码中传递的
$regionList
集合,输出每个元素的属性。
- 条件判断 :
#if($score>80)
、#elseif($score>60)
和#else
:使用#if
指令进行条件判断,根据不同的条件输出不同的内容。
总结
通过 Java 代码将数据传递给 Velocity 模板引擎,模板文件中使用 Velocity 指令进行变量定义、对象属性访问、循环遍历和条件判断,最终生成动态的 HTML 文件。这种方式使得视图和数据分离,提高了代码的可维护性和可扩展性。