velocity的基础入门学习
什么是velocity
velocity是一个基于java的模板引擎,可以通过特定的语法获取在java对象的数据,填充到模板中,从而实现界面和Java的分离。
应用场景
Web应用场景:作为应用程序的试图,展示数据。
源代码生成:velocity可以用来基于模板生成java源代码
自动电子邮箱:网站注册,认证等的电子邮箱模板
网页静态化:基于velocity模板生成静态网页
快速入门demo
使用velocity定义一个html模板,将动态数据填充到模板中形成一个完整的HTML页面
1.创建项目
2.引入依赖
<!--velocity模板引擎--> <dependency> <groupId>org.apache.velocity</groupId> <artifactId>velocity-engine-core</artifactId> <version>2.3</version> </dependency>
模板的使用后缀名是vm
html.vm
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Title</title> </head> <body> </body> </html>
编写测试类进行测试生成一个html模板
import org.apache.velocity.Template; import org.apache.velocity.VelocityContext; import org.apache.velocity.app.Velocity; import org.junit.jupiter.api.Test; import org.springframework.boot.test.context.SpringBootTest; import java.io.FileWriter; import java.io.IOException; import java.util.Properties; @SpringBootTest class CommonPowerApplicationTests { @Test void contextLoads() throws IOException { //设置velocity的资源加载器 Properties prop = new Properties(); prop.put("file.resource.loader.class", "org.apache.velocity.runtime.resource.loader.ClasspathResourceLoader"); //初始化velocity引擎 Velocity.init(prop); //创建velocity容器 VelocityContext context = new VelocityContext(); context.put("name","张三"); //加载velocity模板文件 Template template = Velocity.getTemplate("templates/html.vm","UTF-8"); //合并数据到模板 FileWriter fw = new FileWriter("C:\\Users\\MI\\Desktop\\ruoyi\\yy-power\\src\\main\\resources\\html\\html.html"); template.merge(context,fw); //释放资源 fw.close(); } }
VTL注释
注释允许在模板中包含描述文字,而这些文字不会被放置到模板引擎的输出中。注释是一种有效的提醒自己和向别人解释你的VTL语句要做什么事情的方法。你也可以把注释用来做其他你认为有用的用途。下面就是一个注释的例子。
语法
行注释
## 注释内容
块注释
#* 块注释内容1 块注释内容2 *#
文档注释
#** 文档注释内容1 文档注释内容2 *#
VTL非解析内容
#[[ 非解析内容1 非解析内容2 ]]#
VTL变量引用
$变量名,若上下文没有对应的变量,则输出字符串”$变量名“
${变量名}, 若上下文中没有对应的变量,则输出字符串”${变量名}“
$!变量名,若上下文中没有对应的变量,则输出空字符串""
$!{变量名},若上下文中没有对应的变量,则输出空字符串""
VTL变量
常规变量:$name 正规语法:${name} ##以上二种,如果获取的变量不存在,在原样显示表达式,如果想展示为”“,可以使用$!变量名 常规变量:$!name 正规语法:$!{name}
测试类中ontext.put("name","张三");引用了变量。注释后第二种变量会为空第一种则会打印$name
VTL方法的引用
写一个实体类
public class User { private String username; private String password; private String email; public String getUsername() { return username; } public void setUsername(String username) { this.username = username; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } public String getEmail() { return email; } public void setEmail(String email) { this.email = email; } }
使用测试类进行引用
常规语法:$user.username ------$user.password----$user.email 正规语法:${user.username} ------${user.password}----${user.email} 常规语法:$!user.username ------$!user.password----$!user.email 正规语法:$!{user.username} ------$!{user.password}----$!{user.email}
方法引用
方法引用实际就是指方法调用操作,关注点和参数方法的返回值将输出到最终的结果中
语法
方法的引用 常规写法: $str ----$now 正规写法: ${str} ----${now} 常规写法: $str.split("")---$now.getTime(), 正规写法: ${str.split("")}---------${now.getTime()} 方法的引用 常规写法: $str ----$now 正规写法: ${str} ----${now} 常规写法: $str.split(" ")---$now.getTime(), 正规写法: ${str.split(" ")}---------${now.getTime()} 常规写法: $str.split(" ")---$now.getTime(), 正规写法: ${str.split(" ")}---------${now.getTime()} 常规写法: $!str.split1(" ")---$!now1.getTime() 正规写法: $!{str.split1(" ")}---------$!{now1.getTime()}
测试类的使用
@Test public void contextLoads() throws IOException { //设置velocity的资源加载器 Properties prop = new Properties(); prop.put("file.resource.loader.class", "org.apache.velocity.runtime.resource.loader.ClasspathResourceLoader"); //初始化velocity引擎 Velocity.init(prop); //创建velocity容器 VelocityContext context = new VelocityContext(); context.put("str","hello word velocity !"); context.put("now",new Date()); /* User user = new User(); user.setUsername("张三"); user.setPassword("123456"); user.setEmail("zhangsan@163.com"); context.put("user",user); */ //context.put("name","张三"); //加载velocity模板文件 Template template = Velocity.getTemplate("templates/html.vm","UTF-8"); //合并数据到模板 FileWriter fw = new FileWriter("C:\\Users\\MI\\Desktop\\ruoyi\\yy-power\\src\\main\\resources\\html\\html.html"); template.merge(context,fw); //释放资源 fw.close(); }
VIT指令----#set
指令主要用于定义重要模块,引用外部资源、流程控制、指令以#作为起始字符
流程控制
#set
作用:在页面中声明定义变量
语法:#set($变量=值)
示例
#获取set指令定义的变量 字符串: $str -----${str} 字符串: $str2 -----${str2} 整形: $int -----${int} 数组类型: $arr -----${arr} 布尔类型: $boolean -----${boolean} map.key1: $map.key1----${map.key1} map.key2: $map.key2----${map.key2} map.key3: $map.key3----${map.key3}
#if
#set($language="java") #if($language.equals("java")) java开发工程师 #elseif($language.equals("php")) php开发工程师 #else 开发工程师 #end
#foreach
作用遍历循环数组和集合
格式:
#foreach($item in $items)
......
[#break]
#end
$items:需要遍历的对象和集合
如果items的类型为map集合,那么遍历的是map的value
$item 变量名称,代表遍历的每一项
#break 退出循环
内置属性:
$foreach.index 获取遍历的索引,从0开始
$foreach.count 获取遍历的次数,从1开始
测试类的使用
@SpringBootTest class CommonPowerApplicationTests { @Test public void contextLoads() throws IOException { //设置velocity的资源加载器 Properties prop = new Properties(); prop.put("file.resource.loader.class", "org.apache.velocity.runtime.resource.loader.ClasspathResourceLoader"); //初始化velocity引擎 Velocity.init(prop); //创建velocity容器 VelocityContext context = new VelocityContext(); context.put("str","hello word velocity !"); context.put("now",new Date()); //context.put("name","张三");//初始化输出直接写死 /* //使用实体类的优势可以从数据库获取 User user = new User(); user.setUsername("张三"); user.setPassword("123456"); user.setEmail("zhangsan@163.com"); context.put("user",user); */ //循环一个集合 //字符串数据 String[] hobbies = new String[]{"eat", "drink", "play", "happy"}; context.put("hobbies",hobbies); //对象集合 ArrayList<User> list = new ArrayList<>(); list.add(new User("yangguo","123456","yangguo@163.com")); list.add(new User("xiaolongnv","123456","xiaolongnv@163.com")); list.add(new User("guofurong","123456","guofurong@163.com")); list.add(new User("lisi","123456","lisi@163.com")); context.put("list",list); //map HashMap<String, Object> map = new HashMap<>(); map.put("key1","value1"); map.put("key2","value2"); map.put("key3","value3"); map.put("key4","value4"); context.put("map",map); //加载velocity模板文件 Template template = Velocity.getTemplate("templates/html.vm","UTF-8"); //合并数据到模板 FileWriter fw = new FileWriter("C:\\Users\\MI\\Desktop\\ruoyi\\yy-power\\src\\main\\resources\\html\\html.html"); template.merge(context,fw); //释放资源 fw.close(); } }
示例:
##使用 <h1>遍历字符串数组</h1> ##查询字符串数组 #foreach($hobby in $hobbies) $foreach.index----$foreach.count----$hobby #end <h1>遍历对象集合</h1> <table border="1px"> <tr> <td>编码</td> <td>用户名</td> <td>密码</td> <td>邮箱</td> <td>操作</td> </tr> #foreach($user in $list) <tr> <td>$foreach.count</td> <td>$user.username</td> <td>$user.password</td> <td>$user.email</td> <td> <a href="">编辑</a> <a href="">删除</a> </td> </tr> #end '</table> <h1>遍历map集合</h1> <h2>遍历值</h2> #foreach($value in $map) $value #end <h2>遍历键值对</h2> #foreach($entry in $map.entrySet()) $entry.key ------$entry.value #end
#include指令
#include
作用:引入外部资源,引入的资源不会被引擎解析
语法:
#include(resource) resource以为单引号或者双引号的字符串,也可以为$变量,内容为外部资源路径 注意的是:如果路径为相对路径,则引擎配置的文件加载路径作为参考
示例
#include("templates/html.vm")
#parse
作用:引入外部资源,引用的资源被引擎所解析
语法:
#parse(resource) resource以为单引号或者双引号的字符串,也可以为$变量,内容为外部资源路径 注意的是:如果路径为相对路径,则引擎配置的文件加载路径作为参考
示例:
#parse("templates/html.vm")
#define
作用:定义重用模板(不带参数)
语法:
#define($模块名称) 模板内容 #end
示例:
#define($table) <table border="1px"> <tr> <td>编码</td> <td>用户名</td> <td>密码</td> <td>邮箱</td> <td>操作</td> </tr> <tr> <td>1</td> <td>yangguo</td> <td>123456</td> <td>yangguo@163.com</td> <td> <a href="">编辑</a> <a href="">删除</a> </td> </tr> <tr> <td>2</td> <td>xiaolongnv</td> <td>123456</td> <td>xiaolongnv@163.com</td> <td> <a href="">编辑</a> <a href="">删除</a> </td> </tr> <tr> <td>3</td> <td>guofurong</td> <td>123456</td> <td>guofurong@163.com</td> <td> <a href="">编辑</a> <a href="">删除</a> </td> </tr> <tr> <td>4</td> <td>lisi</td> <td>123456</td> <td>lisi@163.com</td> <td> <a href="">编辑</a> <a href="">删除</a> </td> </tr> </table> #end ##引用定义好的模块 $table $table $table $table
#evaluate
语法:
<h1>动态计算</h1> #set($name="over") #evaluate("#if($name=='over') over #else not over #end") #if($name=='over') over #else not over #endc
测试类
@Test public void test5() throws IOException { //设置velocity的资源加载器 Properties prop = new Properties(); prop.put("file.resource.loader.class", "org.apache.velocity.runtime.resource.loader.ClasspathResourceLoader"); //初始化velocity引擎 Velocity.init(prop); //创建velocity容器 VelocityContext context = new VelocityContext(); context.put("code","#if($language.equals(\"java\"))java开发工程师#elseif($language.equals(\"php\"))php开发工程师#else开发工程师#end"); //加载velocity模板文件 Template template = Velocity.getTemplate("templates/cs.vm","UTF-8"); //合并数据到模板 FileWriter fw = new FileWriter("C:\\Users\\MI\\Desktop\\ruoyi\\yy-power\\src\\main\\resources\\html\\cs.html"); template.merge(context,fw); //释放资源 fw.close(); }
vm的用法
#evaluate($code)
宏指令
作用:定义重用模块(可带参数)
语法:
定义语法:
#macro(宏名[$arg]?) ........ #end
调用语法:
#宏名([$arg]?)
示例:
测试类的用法
@Test public void test6() throws IOException { //设置velocity的资源加载器 Properties prop = new Properties(); prop.put("file.resource.loader.class", "org.apache.velocity.runtime.resource.loader.ClasspathResourceLoader"); //初始化velocity引擎 Velocity.init(prop); //创建velocity容器 VelocityContext context = new VelocityContext(); //对象集合 ArrayList<User> list = new ArrayList<>(); list.add(new User("yangguo","123456","yangguo@163.com")); list.add(new User("xiaolongnv","123456","xiaolongnv@163.com")); list.add(new User("guofurong","123456","guofurong@163.com")); list.add(new User("lisi","123456","lisi@163.com")); context.put("list",list); //加载velocity模板文件 Template template = Velocity.getTemplate("templates/cs3.vm","UTF-8"); //合并数据到模板 FileWriter fw = new FileWriter("C:\\Users\\MI\\Desktop\\ruoyi\\yy-power\\src\\main\\resources\\html\\cs3.html"); template.merge(context,fw); //释放资源 fw.close(); } }
vm的用法
#macro(table $list) <table border="1px"> <tr> <td>编码</td> <td>用户名</td> <td>密码</td> <td>邮箱</td> <td>操作</td> </tr> #foreach ($user in $list) <tr> <td>$foreach.count</td> <td>$user.username</td> <td>$user.password</td> <td>$user.email</td> <td> <a href="">编辑</a> <a href="">删除</a> </td> </tr> #end </table> #end ## 引用定义好的模块 #table($list)
本文来自博客园,作者:我是一个小仓鼠,转载请注明原文链接:https://www.cnblogs.com/yongyuankuaile/p/16374401.html