Apache Velocity 是一个基于java的模板引擎(template engine)

应用场景
1.Web 应用:开发者在不使用 JSP 的情况下,可以用 Velocity 让 HTML 具有动态内容的特性
2.源代码生成:Velocity 可以被用来生成 Java 代码、SQL

语法概要
1.在 Velocity 中所有的关键字都是以#开头的,而所有的变量则是以$开头。

2.关系和逻操作符
Velocity 也具有逻辑AND, OR 和 NOT 操作符。

3.在Velocity中可以使用循环语法遍历集合,语法结构如下:

#foreach($item in $list)
$item
$velocityCount
#end

4.在Velocity中可以使用条件语法对流程进行控制

#if(condition)
...dosonmething...
#elseif(condition)
...dosomething...
#else
...dosomething...
#end

5.#include和#parse的作用都是引入本地文件, 为了安全的原因,被引入的本地文件只能在TEMPLATE_ROOT目录下。

区别:

与#include不同的是,#parse只能指定单个对象。而#include可以有多个

如果您需要引入多个文件,可以用逗号分隔就行:

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

#include被引入文件的内容将不会通过模板引擎解析;而#parse引入的文件内容Velocity将解析其中的velocity语法并移交给模板,意思就是说相当与把引入的文件copy到文件中。

velocity自定义功能标签:

1.加载设置

public VelocityServiceImpl() {
VelocityEngineFactoryBean factory = new VelocityEngineFactoryBean();
Properties property = new Properties();
property.setProperty("resource.loader", "class");
property.setProperty("class.resource.loader.class",
"org.apache.velocity.runtime.resource.loader.ClasspathResourceLoader");
property.setProperty("input.encoding", DEFAULT_ENCODING);
property.setProperty("output.encoding", DEFAULT_ENCODING);
property.setProperty(Velocity.EVENTHANDLER_REFERENCEINSERTION,
"com.sinolife.bep.common.util.VelocityReferenceHandler");
//自定义标签
property.setProperty("userdirective","com.sinolife.bep.common.util.UnTransferDirective");
factory.setVelocityProperties(property);
factory.setResourceLoader(PlatformContext.getGoalbalContext(ResourceLoader.class, ResourceLoader.class));
try {
factory.afterPropertiesSet();
} catch (Exception e) {
throw new RuntimeException("Instal LocalLoginHander Error", e);
}
velocityEngine = factory.getObject();
}

2.定义标签

/**
* 自定义不转义标签,
* 功能:velocity使用此标签输出的值不会做转义处理
* 模板使用:#unTransfer("参数1")
* 如:#unTransfer("<//123>")
* 会直接输出<//123>到页面,不做转义。
* @author zhonglihai
*
*/
public class UnTransferDirective extends Directive{
@Override
public String getName() {
return "unTransfer";//自定义指令的名称
} @Override
public int getType() {
return 2;//配置不带结束符
} @Override
  public boolean render(InternalContextAdapter context,
Writer writer, Node node) throws IOException,
ResourceNotFoundException, ParseErrorException,
MethodInvocationException {
try {
String str = node.jjtGetChild(0).value(context).toString();//第一个值
writer.write(str.toString());//输出结果
} catch (Exception e) {
return false;
}
return true;
  }
}

3.标签使用

--Velocity引用解析控制器
public class VelocityReferenceHandler implements ReferenceInsertionEventHandler { public Object referenceInsert(String key, Object value) {
if (value instanceof java.lang.String) {
value = VelocityUtils.transeChar((String) value);
}
return value;
} }
--工具类,转义字符
public class VelocityUtils { /**
* 把字符串里面的特殊字符进行转换
*
* @param str
* @return
*/
public static String transeChar(String str) {
if (StringUtils.isNotBlank(str)) {
str = str.replaceAll("<", "<").replaceAll(">", ">").replaceAll("&", "&")
.replaceAll("'", "'").replaceAll("\"", """);
} return str;
}
}
--模板页面使用
<!-- 4.发送OA的单据HTML快照 5.发送OA的单据PDF快照 采用<![CDATA[ ]]> 包裹-->
#if($filterMask.operId != "2" && $filterMask.operId != "3")
#if($filterMask.operId == "4" || $filterMask.operId == "5")
<td><![CDATA[#unTransfer($!{billWorkFlowHistoryDomain.dealOpinion})]]></td>
#else
<td>#unTransfer($!{billWorkFlowHistoryDomain.dealOpinion})</td>
#end
#end

  


 

velocity 相关的更多相关文章

  1. 点我吧工作总结(技术篇) Velocity

    1. 什么是velocity Velocity[vəˈlɑ:səti],名称字面翻译为:速度.速率.迅速.该项目的开源地址:http://velocity.apache.org/,它是一个基于Java ...

  2. Velocity模板引擎入门

    类似于PHP中的Smarty,Velocity是一个基于Java的模板引擎(template engine).它允许任何人仅仅简单的使用模板语言(template language)来引用由java代 ...

  3. 【转载】Velocity模板引擎的介绍和基本的模板语言语法使用

    原文地址http://www.itzhai.com/the-introduction-of-the-velocity-template-engine-template-language-syntax- ...

  4. SpringMVC 学习-如何搭配使用 Velocity 页面模板

    一.快速搭建 Velocity 框架 1. 加入所需 Jar 包 <dependency> <groupId>org.apache.velocity</groupId&g ...

  5. eclipse开发velocity实例(初学)

    开发环境         Eclipse Java EE IDE for Web Developers.(Version: Helios Service Release 1) jdk1.6.0_07 ...

  6. 【转】解决高版本springboot对Velocity不支持

    https://blog.csdn.net/sinat_31270499/article/details/82283880 最近在做关于Spring Boot开发的项目,因为项目中要用到Velocit ...

  7. 项目编码规范(Ali)

    一.研发流程规范 二.SQL编码规范 数据库命名规范:数据库名一律小写,必须以字母开头.库名包含多个单词的,以下划线“_”分隔.如果采用分库方案,分库编号从“0”开始,用“0”左补齐为四位. 表名规范 ...

  8. 【solr专题之四】关于VelocityResponseWriter

    一.关于Velocity的基本配置 在Solr中,可以以多种方式返回搜索结果,如单纯的文本回复(XML.JSON.CSV等),也可以返回velocity,js等格式.而VelocityResponse ...

  9. 【solr这四个主题】大约VelocityResponseWriter

    一个.大约Velocity基本配置 在Solr在,可以以多种方式返回搜索结果,作为一个简单的文字回复(XML.JSON.CSV等待),能够返回velocity.js等格式.而VelocityRespo ...

随机推荐

  1. docker-compose的scale的用法

    用到的三个文件 docker-compose version: " services: redis: image: redis web: build: context: . dockerfi ...

  2. Java面试必问-ThreadLocal

    前言 在面试环节中,考察"ThreadLocal"也是面试官的家常便饭,所以对它理解透彻,是非常有必要的. 有些面试官会开门见山的提问: “知道ThreadLocal吗?” “讲讲 ...

  3. SQL必知必会——思维导图

    Xmind实在太坑了,竟然不能导出高清图片,我回来折腾个PS整一下!

  4. 39-python基础-python3-字典常用方法-get()

    在访问一个键的值之前,检查该键是否存在于字典中,这很麻烦. 好在,字典有一个 get()方法,它有两个参数:要取得其值的键,以及如果该键不存在时,返回的备用值. dict.get(键,默认值) 实例- ...

  5. shell位置参数和专用参数举例

  6. 2016计蒜之道复赛 百度地图的实时路况 分治+Floyd

    题目链接:https://nanti.jisuanke.com/t/A1108 这道题还挺有意思的.让我对Floyd的了解又加深了一点. 首先我们重新审视Floyd这三重循环到底有什么用?第一层是枚举 ...

  7. visual studio 自定义警告标签

    写代码中经常会遇见某段代码写的不大合适或者是有隐患,但是一时半会有没时间不去完善,总体上不影响程序,也就放下了 结果时间一久就给忘了 vs提供了自定义警告的功能,这样就能有个提醒啦,方便以后改进 us ...

  8. oscache缓存

    oscache 使用总结 Posted on 2009-05-22 22:45 青果 阅读(1270) 评论(2)  编辑  收藏 所属分类: 技术点滴  前阵子对公司网站进行了性能优化,其中,有一项 ...

  9. The 'with' and 'as' Keywords

    Programming is all about getting the computer to do the work. Is there a way to get Python to automa ...

  10. JMeter目录结构

    转载自https://www.cnblogs.com/imyalost/p/6959797.html 首先得了解一下这些东西,以后才能快速的找到某些配置文件进行修改(举个例子,改配置只是其中之一) 一 ...