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的源码,但是 ...
随机推荐
- Floyd's Cycle Detection Algorithm
Floyd's Cycle Detection Algorithm http://www.siafoo.net/algorithm/10 改进版: http://www.siafoo.net/algo ...
- 3DsMax动画插件
* 简易骨骼动画: Mesh当前帧顶点 = Mesh绑定时顶点 * 绑定时骨骼的变换到本帧骨骼的变换的改变量. = Mesh绑定时顶点 * 绑定时骨骼的变换的逆矩阵 * 本帧的骨骼变换. = Mesh ...
- Cookie用法
//写入 protected void Button1_Click(object sender, EventArgs e) { HttpCookie cookie=new HttpCookie(&qu ...
- 语义版本号(Semantic Versioning)
版本号格式不陌生吧,.NET 传统的版本号格式类似这样 1.5.1254.0.本文将推荐一种新的版本号格式——语义版本号,格式类似这样 1.4.6-beta.我推荐语义版本号是因为这样的版本号自包含语 ...
- WPF/UWP 绑定中的 UpdateSourceTrigger
在开发 markdown-mail 时遇到了一些诡异的情况.代码是这么写的: <TextBox Text="{Binding Text, Mode=TwoWay}"/> ...
- .NET程序下载获得的ContentLength=-1
你写的.NET(C#)下载程序是否会遇到过这样的问题?--ContentLength=-1. 例如,有如下代码: HttpWebRequest webRequest = (HttpWebRequest ...
- maven-assembly-plugin 打包简单案例
简单项目 1. maven netty lomback 包含项目依赖 <dependencies> <dependency> <groupId>io.ne ...
- openresty websocket 使用
openresty websocket 使用 1. 代码如下: local server =require"resty.websocket.server" local wb, ...
- mui.fire 目标页无法监听到 触发事件
//获得详情页面 if(!detailPage){ detailPage = plus.webview.getWebviewById('detail.html'); } //触发详情页面的newsId ...
- Navicat Premium 连接Oracle 出现ora-12505 错误解决方案
找到listener.ora文件:我的Oracle是安装在F盘, 路径为:F:\oracle\product\10.2.0\db_1\network\admin\listener.ora 改动前的配 ...