https://blog.csdn.net/icannotdebug/article/details/79725297

一、简介

Velocity 是一个基于 Java 的模板引擎框架,提供的模板语言可以使用在 Java 中定义的对象和变量上
说白了就类似于jsp,java中定义对应的对象,模板载入后,可以对象信息动态加载入模板

二、使用方式

1、引入pom(老生常谈)

<dependency>
<groupId>org.apache.velocity</groupId>
<artifactId>velocity-engine-core</artifactId>
<version>2.0</version>
</dependency>

最新的开发包和maven配置

2、创建模板文件(Hellovelocity.vm)

#set( $iAmVariable = "good!" )
Welcome $name to velocity.com
today is $date.
#foreach ($i in $list)
$i
#end
$iAmVariable

3、编写测试main

import org.apache.velocity.Template;
import org.apache.velocity.VelocityContext;
import org.apache.velocity.app.VelocityEngine;
import org.apache.velocity.runtime.RuntimeConstants;
import org.apache.velocity.runtime.resource.loader.ClasspathResourceLoader; import java.io.StringWriter;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
/**
* @author: <a href="mailto:wb-lzl282164@alibaba-inc.com">李智龙</a>
* @date: 2018/3/28
*/
public class HelloVelocity {
public static void main(String[] args) {
VelocityEngine ve = new VelocityEngine();
ve.setProperty(RuntimeConstants.RESOURCE_LOADER, "classpath");
ve.setProperty("classpath.resource.loader.class", ClasspathResourceLoader.class.getName());
ve.init();
// 载入(获取)模板对象
Template t = ve.getTemplate("hellovelocity.vm");
VelocityContext ctx = new VelocityContext();
// 域对象加入参数值
ctx.put("name", "李智龙");
ctx.put("date", (new Date()).toString());
// list集合
List temp = new ArrayList();
temp.add("1");
temp.add("2");
ctx.put("list", temp); StringWriter sw = new StringWriter();
t.merge(ctx, sw); System.out.println(sw.toString());
}
}

4、打印结果

Welcome 李智龙 to velocity.com
today is Wed Mar 28 11:03:04 CST 2018.
1
2
good!

三、使用模板的目的

如果说使用模板生成java代码,或者开发对应的代码,目前有很多封装好的更好用的开源框架,

使用Velocity自己编写一个适用的框架也是可以的(很蛋疼吧)。
不过个人觉得Velocity最好的使用场景,是在文件模板的生成方面,现在有很多场景需要打印报表,

生成对应的文件,而Velocity便是一个过渡的比较好用的轻量的插件。后面也会提供相应的例子

Velocity模板引擎实战:动态生成HTML、Word、Excel等报表

四、模板的基本语法

1、定义变量

#set($name =“Jackie”)
#set($hello =“hello $name”)
Jackie赋值给$name,此时$hello的值为hello Jackie

2、变量使用

模板文件可以使用name或

{name}定义变量,建议使用后者,name和

names 的两个变量,如果不选用大括号的话,引擎就没有办法正确识别$names 这个变量。

3、循环

#foreach($element in $list)
This is $element
$velocityCount
#end

4、条件语句

#if(condition)
...
#elseif(condition)

#else

#end

5、关系操作符

AND、OR 和 NOT 操作符,分别对应&&、||和! 

6、函数

#macro(macroName arg1 arg2 …)
...
#end
// 对应函数
#macroName(arg1 arg2 …)

示例

#macro(sayHello $name)
hello $name
#end
#sayHello(“velocity”)

7、解析和引入模板

temp.vm 文件:

#set($name =“velocity”)

parse.vm:

#parse(“temp.vm”)
输出结果为:velocity

include.vm:

#include(“temp.vm”)
输出结果为:#set($name =“velocity”)

Java使用 VelocityEngine模板引擎快速生成HTML等各种代码的更多相关文章

  1. 使用 Velocity 模板引擎快速生成代码(zhuan)

    http://www.ibm.com/developerworks/cn/java/j-lo-velocity1/ ****************************************** ...

  2. 使用Velocity 模板引擎快速生成代码

    Velocity 模板引擎介绍 在现今的软件开发过程中,软件开发人员将更多的精力投入在了重复的相似劳动中.特别是在如今特别流行的MVC架构模式中,软件各个层次的功能更加独立,同时代码的相似度也更加高. ...

  3. 转: 使用 Velocity 模板引擎快速生成代码

    from:https://www.ibm.com/developerworks/cn/java/j-lo-velocity1/ 评注: 1. velocity 的基本语法 2. 生成代码的用法.

  4. 使用 Velocity 模板引擎快速生成代码

    http://www.ibm.com/developerworks/cn/java/j-lo-velocity1/

  5. SpringBoot获取Freemarker模板引擎,生成HTML代码

    今天用Ajax异步添加评论,加载Freemarker模板引擎,生成模板模块 1.新建Freemarker模板 <li id="${comment.oId}"> < ...

  6. 使用VTemplate模板引擎动态生成订单流程图

    1.VTemplate模板引擎的简介 VTemplate模板引擎也简称为VT,是基于.NET的模板引擎,它允许任何人使用简单的类似HTML语法的模板语言来引用.NET里定义的对象.当VTemplate ...

  7. 使用Themleaf 模板引擎手动生成html文件

    1.为什么要写这一篇呢? 在做一个邮件发送功能的时候,需要发送html邮件,javaMail 发送html 的时候需要有已经生成的html正文,所以需要提前将要发送的内容生成,所以就需要模板引擎来动态 ...

  8. mybatis配eclise模板,mybatis快速生成模板

    eclipse中mybatis得mapper文件不提示(mybatis-3-mapper.dtd,mybatis-3-config.dtd) 1.下载该文件到你的硬盘文件夹下 2.windows -- ...

  9. 写一个迷你版Smarty模板引擎,对认识模板引擎原理非常好(附代码)

    前些时间在看创智博客韩顺平的Smarty模板引擎教程,再结合自己跟李炎恢第二季开发中CMS系统写的tpl模板引擎.今天就写一个迷你版的Smarty引擎,虽然说我并没有深入分析过Smarty的源码,但是 ...

随机推荐

  1. 在父容器div中图片下方有一条空隙问题

    问题:<div><img src="mm1.jpg"></div> 然后,表现就是一张图片呈现,类似下面这样: 恩,看上去很正常,一切都是理所当 ...

  2. LOJ2362. 「NOIP2016」蚯蚓【单调队列】

    LINK 思路 良心来说这题还挺思维的 我没看题解也不知道要这样维护 把每次斩断的点分别放进两个队列里面 因为要维护增长,所以可以让新进队的节点来一个负增长? 是不是就好了? 然后很容易发现因为在原始 ...

  3. Luogu1155 NOIP2008 双栈排序 【二分图染色】【模拟】

    Luogu1155 NOIP2008 双栈排序 题目描述 Tom最近在研究一个有趣的排序问题.如图所示,通过 2个栈 S1 和 S2 ,Tom希望借助以下 44 种操作实现将输入序列升序排序. 操作 ...

  4. BZOJ1013 JSOI2008 球形空间产生器sphere 【高斯消元】

    BZOJ1013 JSOI2008 球形空间产生器sphere Description 有一个球形空间产生器能够在n维空间中产生一个坚硬的球体.现在,你被困在了这个n维球体中,你只知道球面上n+1个点 ...

  5. jenkins for mac

    Mac环境中Jenkins的停止和启动命令启动sudo launchctl load /Library/LaunchDaemons/org.jenkins-ci.plist停止sudo launchc ...

  6. 《DSP using MATLAB》示例Example 8.13

    %% ------------------------------------------------------------------------ %% Output Info about thi ...

  7. Cookie Session 和Django分页

    cookie Cookie的由来 大家都知道HTTP协议是无状态的. 无状态的意思是每次请求都是独立的,它的执行情况和结果与前面的请求和之后的请求都无直接关系,它不会受前面的请求响应情况直接影响,也不 ...

  8. MySQL出现1030-Got error 28 from storage engine错误

    Navicat for MySQL出现1030-Got error 28 from storage engine错误  刚刚还能用这会儿就用不了了,估计是磁盘空间不足引起的! 在根目录/下执行命令:d ...

  9. RK3288 通过指令查看当前显示内容(framebuffer)

    $ adb shell root@xxx:/ # cd /dev/graphics cd /dev/graphics root@xxx:/dev/graphics # ls ls fb0 fb1 fb ...

  10. Clustershell集群管理

    在运维实战中,如果有若干台数据库服务器,想对这些服务器进行同等动作,比如查看它们当前的即时负载情况,查看它们的主机名,分发文件等等,这个时候该怎么办?一个个登陆服务器去操作,太傻帽了!写个shell去 ...