Java使用 VelocityEngine模板引擎快速生成HTML等各种代码
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>
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等各种代码的更多相关文章
- 使用 Velocity 模板引擎快速生成代码(zhuan)
http://www.ibm.com/developerworks/cn/java/j-lo-velocity1/ ****************************************** ...
- 使用Velocity 模板引擎快速生成代码
Velocity 模板引擎介绍 在现今的软件开发过程中,软件开发人员将更多的精力投入在了重复的相似劳动中.特别是在如今特别流行的MVC架构模式中,软件各个层次的功能更加独立,同时代码的相似度也更加高. ...
- 转: 使用 Velocity 模板引擎快速生成代码
from:https://www.ibm.com/developerworks/cn/java/j-lo-velocity1/ 评注: 1. velocity 的基本语法 2. 生成代码的用法.
- 使用 Velocity 模板引擎快速生成代码
http://www.ibm.com/developerworks/cn/java/j-lo-velocity1/
- SpringBoot获取Freemarker模板引擎,生成HTML代码
今天用Ajax异步添加评论,加载Freemarker模板引擎,生成模板模块 1.新建Freemarker模板 <li id="${comment.oId}"> < ...
- 使用VTemplate模板引擎动态生成订单流程图
1.VTemplate模板引擎的简介 VTemplate模板引擎也简称为VT,是基于.NET的模板引擎,它允许任何人使用简单的类似HTML语法的模板语言来引用.NET里定义的对象.当VTemplate ...
- 使用Themleaf 模板引擎手动生成html文件
1.为什么要写这一篇呢? 在做一个邮件发送功能的时候,需要发送html邮件,javaMail 发送html 的时候需要有已经生成的html正文,所以需要提前将要发送的内容生成,所以就需要模板引擎来动态 ...
- mybatis配eclise模板,mybatis快速生成模板
eclipse中mybatis得mapper文件不提示(mybatis-3-mapper.dtd,mybatis-3-config.dtd) 1.下载该文件到你的硬盘文件夹下 2.windows -- ...
- 写一个迷你版Smarty模板引擎,对认识模板引擎原理非常好(附代码)
前些时间在看创智博客韩顺平的Smarty模板引擎教程,再结合自己跟李炎恢第二季开发中CMS系统写的tpl模板引擎.今天就写一个迷你版的Smarty引擎,虽然说我并没有深入分析过Smarty的源码,但是 ...
随机推荐
- fedora22 安装fcitx 输入法
<h4>安装fcitx:</h4><blockquote>sudo yum install fcitx fcitx-pinyin fcitx-configtools ...
- PHP 读取和导出 CSV文件
PHP 读取和导出 CSV文件,速度比phpexcel快80%,而phpexcel 占内存,在数据大的情况下,5万条只需几秒不到,几乎感觉不出来 如果遇到数字是科学计算法,可以在前面加一个 ' 单引号 ...
- flask第十三篇——url注意事项
先和大家说一下常用的请求方法 get:只是从服务器获取数据,不会对服务器的状态或数据产生任何影响: get方法的参数是放在URL中传递的. post:会对服务器的状态或数据产生影响:通过body形式进 ...
- asp.net远程调用WebService的两种方法(转载)
一,静态方法在“解决方案‘项目名’” -> 相应的文件夹,如“Web References” ->右键“添加WEB引用”->在URL里写入地址.二,动态方法在“解决方案‘项目名’” ...
- grpc nodejs tools 安装问题
grpc nodejs 应用安装提示错误: stack Error: EPERM: operation not permitted, utime '/usr/local/lib/node_modul ...
- Codeforces Round #249 (Div. 2)-D
这场的c实在不想做,sad. D: 标记一下每个点8个方向不经过黑点最多能到达多少个黑点. 由题意可知.三角形都是等腰三角形,那么我们就枚举三角形的顶点. 对于每个定点.有8个方向能够放三角形. 然后 ...
- c++重在运算符前置自增和后置自增
class student { int age; }; int main() { class student stu; (stu++)++;//error ++(stu++);//error stu+ ...
- Linux之 iostat 解读磁盘io
1.iostat[oracle@orastb log]$ iostatLinux 3.10.0-327.el7.x86_64 (orastb.bonc.com.cn) 09/07/2017 _x86_ ...
- wordpress域名解析到了网站,但是点击其他页面会出现ip而不是域名
1.前提域名可以访问你的网站证明解析没问题 2.那就是wp后台的设置问题,将url和站点url改为你的域名http://www.eovision.cc清理缓存即可 亲测可用,如果改了出现页面 ...
- Java NIO简单介绍(一)
Java NIO( New IO) 是从Java 1.4版本开始引入的 一个新的IO API,可以替代标准的Java IO API. NIO与原来的IO有同样的作用和目的,但是使用的方式完全不同,NI ...