文章目录
1. 注释
- 1. 注释
- 2. 定义变量
- 3. 条件语句
- 4. 循环标签
- 5. Velocity 中的宏
- 6. debug语法标签
- 7. 不存在的变量显示为空白
- 8. 本地文件引入
- 9. 调用类方法
单行注释 :
## 内容
多行注释:
#* 内容 *#
文档注释:
#**内容*#
2. 定义变量
可以使用#set
来定义变量 ,举个例子:
① 模板内容:
#set($word="zhuoqianmingyue")
${word}#$word
#set($surname="Lee")
#set($name="junkui")
#set($fullname="$surname $name")
${fullname}
② 单元测试方法:
public void set() {
String templatePath = "cn/lijunkui/api/set.vm";
String htmlPath = "D:\\set.html";
test(templatePath,htmlPath);
}
③ 测试类
private void test(String templatePath, String htmlPath) {
VelocityEngine ve=new VelocityEngine();
//设置模板加载路径,这里设置的是class下
ve.setProperty(Velocity.RESOURCE_LOADER, "class");
ve.setProperty("class.resource.loader.class", "org.apache.velocity.runtime.resource.loader.ClasspathResourceLoader");
try {
//进行初始化操作
ve.init();
//加载模板,设定模板编码
Template t=ve.getTemplate(templatePath,"utf-8");
//设置初始化数据
VelocityContext context = new VelocityContext();
//设置输出
PrintWriter writer = new PrintWriter(htmlPath);
//将环境数据转化输出
t.merge(context, writer);
writer.close();
} catch (Exception e) {
e.printStackTrace();
}
}
3. 条件语句
语法:
#if #elseif #else #end
下面为模板内容示例:
#set($number = 1)
#if($number == 1)
这个数是 1
#elseif($number == 2)
这个数是 2
#else
这个数是3
#end
4. 循环标签
循环标签可以使用#foreach
举例:
#foreach ( $item in [1..5] )
$item
#end
-------------------------------
#set ( $arr = [0..1] )
#foreach ( $x in $arr )
$x
#end
-------------------------------
#set ( $arr2 = [0..1] )
#set ( $k = 1 )
#foreach ( $x in $arr2 )
x:$x,k: $k
#set($k = $k+1)
#end
-------------------------------
5. Velocity 中的宏
举例:
#macro(html)
这是一个宏
#end
#macro(htmlContent $content)
$content
#end
#html()
#htmlContent("有参数的宏")
6. debug语法标签
debug
语法标签可以使用#stop
,举例:
stop前的内容
#stop
stop后的内容
7. 不存在的变量显示为空白
为了把不存在的变量或变量值为null
的对象$msg
显示为空白,则只需要在变量名前加一个“!”
号即可,举例:
#set($word="zhuoqianmingyue")
存在的内容:${word}#
不存在的内容:${word1}
不存在的内容不显示:$!{word1}
---------------------------
#if($!{word1})
存在
#else
不存在
#end
8. 本地文件引入
#include
: 可以引入多个文件,被引入文件的内容将不会通过模板引擎解析#parse
: 只能引入单个文件,引入的文件内容Velocity
将解析其中的velocity
语法并移交给模板
举例:
#include( "abc.txt","cde.txt" )
#parse("parent.vm")
9. 调用类方法
举例:
① 定义一个String
工具类 将小写内容转换为大写:
package cn.lijunkui.api;
public class StringUtils {
public static String toUpper(String word) {
return word.toUpperCase();
}
}
② 编写模板引擎初始化并将工具类的实例对象加载进模板的上下文中:
public void initWihtFun(String templatePath, String htmlPath) {
VelocityEngine ve=new VelocityEngine();
//设置模板加载路径,这里设置的是class下
ve.setProperty(Velocity.RESOURCE_LOADER, "class");
ve.setProperty("class.resource.loader.class", "org.apache.velocity.runtime.resource.loader.ClasspathResourceLoader");
ve.setProperty(Velocity.FILE_RESOURCE_LOADER_PATH, "D:\\workspace-sts-3.9.5.RELEASE\\velocitylearn\\target\\velocitylearn\\WEB-INF\\classes\\cn\\lijunkui\\api");
try {
//进行初始化操作
ve.init();
//加载模板,设定模板编码
Template t=ve.getTemplate(templatePath,"utf-8");
//设置初始化数据
VelocityContext context = new VelocityContext();
StringUtils stringUtils = new StringUtils();
context.put("stringUtils", stringUtils);
//设置输出
PrintWriter writer = new PrintWriter(htmlPath);
//将环境数据转化输出
t.merge(context, writer);
writer.close();
} catch (Exception e) {
e.printStackTrace();
}
}
③ 编写模板内容:
#set($word="zhuoqianmingyue")
$stringUtils.toUpper("abc")
$stringUtils.toUpper(${word})
④ 测试用例:
public void useFun() {
String templatePath = "cn/lijunkui/api/useFun.vm";
String htmlPath = "D:\\useFun.html";
initWihtFun(templatePath,htmlPath);
}