栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

关于Java的Velocity模板使用

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

关于Java的Velocity模板使用

关于Java的Velocity模板使用
  • 1 Velocity的简介
    • 1 基本语法
      • 1 关键字
      • 2 变量
      • 3 转义字符和逻辑操作符
      • 4 循环
      • 5 条件
      • 6 注释
      • 7 引入资源
  • 2 Velocity的使用

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

1 Velocity的简介

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

1 基本语法 1 关键字

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

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

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

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

{}变量

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

u s e r N a m e , 此 时 页 面 上 可 以 表 示 为 {user}Name, 此时页面上可以表示为 userName,此时页面上可以表示为someoneName的效果.

!变量

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

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

## 定义一个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模板中转义字符是

#set{$user = "李白"}
## 输入		结果
$user		 李白
$user		 $user
\$user		 李白
\$user	 $user

&& 且

|| 或

! 取反

4 循环

Velocity模板中list集合循环语法

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

#foreach ( $element in $list)
	## 集合中每个元素
	$element
	## 集合的序号 从1开始
	${velocityCount}
	## 集合的长度
	${list.size()}
#end

map集合循环语法

#foreach ($entry in $map.entrySet())
## map的key   map的value值
$entry.key => $entry.value
#end
5 条件

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

#if (condition1)
	// 执行业务
#elseif (condition2)
	// 执行业务
#else
	// 执行业务
#end

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

#if (condition1)
	// 执行业务
#else
	// 执行业务
#end

#break

表示跳出循环

#if (condition1)
  ## 条件符合跳过
  #if($user == "李白")
    #break;
  #end
#else
	// 执行业务
#end

#stop

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

#if (condition1)
  ## 条件符合直接终止
  #if($user == "李白")
    #stop
  #end
#else
	// 执行业务
#end
6 注释

单行注释 ##

## 定义一个user变量为李白
#set{$user = "李白"}

多行注释 #* *#

#*  
  定义一个user变量
  将user变量赋值为 李白
*#
#set{$user = "李白"}

文档注释 #** *#

 #** 
 	@version 1.1
 	@author  李白
 *#
#set{$user = "李白"}
7 引入资源

#include

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

#include( "one.gif","two.txt","three.htm" )

#parse

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

##  a.vm文件
#set($user = "李白")


## b.vm文件
#parse("a.vm")
## 变量   值
$user	  李白
2 Velocity的使用

Velocity常用的案例和工具类

public class VelocityUtils {

    public static void main(String[] args) {

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

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

    
    public static void getFile(String templatePath, String templateName, String outFilePath,
            Map 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();
        }
    }
    
   
    public static void createFile(HttpServletResponse response, Map params)
            throws IOException {
        
        ByteArrayOutputStream output = new ByteArrayOutputStream();
        ZipOutputStream outZip = new ZipOutputStream(output);

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

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

        //获取模板列表
        List 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());

    }

    
    
    private static String getFileName(String template) {
        return template.replace(".vm", "");
    }

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

    
    public static void loadTemplateFileByOne() {
        Properties p = new Properties();
        p.put("file.resource.loader.class",
                "org.apache.velocity.runtime.resource.loader.ClasspathResourceLoader");
        Velocity.init(p);
    }

    
    public static void loadTemplateFileByTwo(String templatePath) {
        Properties p = new Properties();
        // 设置模板加载路径 为D盘 work文件夹
        p.setProperty(VelocityEngine.FILE_RESOURCE_LOADER_PATH, templatePath);
        Velocity.init(p);
    }

    
    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文件夹中


#foreach($name in $list)
	
#end
Names:${name}
第${velocityCount}个, 名字为 $name , 总共 ${list.size()} 个

生成的index.html文件

Names:world
第1个, 名字为 李白 , 总共 3 个
第2个, 名字为 杜甫 , 总共 3 个
第3个, 名字为 陆游 , 总共 3 个

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

参考资料:

http://www.51gjie.com/javaweb/896.html

https://yanglinwei.blog.csdn.net/article/details/119185550

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/590812.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号