`
364434006
  • 浏览: 587204 次
  • 性别: Icon_minigender_1
  • 来自: 深圳
社区版块
存档分类
最新评论

Velocity模板引擎入门

 
阅读更多

类似于PHP中的Smarty,Velocity是一个基于java的模板引擎(template engine)。它允许任何人仅仅简单的使用模板语言(template language)来引用由java代码定义的对象。从而实现界面和Java代码的分离,使得界面设计人员可以和java程序开发人员同步开发一个遵循MVC架构的web站点。

另外,Velocity的能力远不止web站点开发这个领域,例如,它可以从模板(template)产生SQL和PostScript、XML,它也可以被当作一个独立工具来产生源代码和报告,或者作为其他系统的集成组件使用。Velocity也可以为Turbine web开发架构提供模板服务(template service)。Velocity+Turbine提供一个模板服务的方式允许一个web应用以一个真正的MVC模型进行开发。


编写Velocity版的Hello World
获取Velocity相关JAR文件:

http://velocity.apache.org/网站上下载最新的Velocity,这里我们下载了velocity-1.7.zip

相关Jar包添加到项目中:

解压velocity-1.7.zip,发下其根目录下有两个JAR文件:

velocity-1.7.jar velocity-1.7-dep.jar

其中velocity-1.7-dep.jar包含了:

velocity-1.7.jar commons-collections-3.2.1.jar commons-lang-2.4.jar oro-2.0.8.jar(这些JAR文件位于解压目录的lib目录下)

在JAR包不冲突的情况下可以直接使用velocity-1.7-dep.jar

载类路径下添加velocity.properties文件:

该文件一般包含如下配置:

 

runtime.log = F:\project\MusicalInstrumentsStore\velocity_example.log
file.resource.loader.path = F:\project\MusicalInstrumentsStore\vm
input.encoding = UTF-8
output.encoding = UTF-8

runtime.log指定日志文件存放位置

 

file.resource.loader.path指定模板的加载位置
input.encoding指定输入编码
output.encoding指定输出编码

 

模版的使用一共以下步骤:

1.初始化模版引擎

2.构建velocity上下文

3.变量值添加到上下文中

4.选择模版

5.合并模版和数据导出到输出流

 

 

//初始化模板引擎
Velocity.init("src/velocity.properties");
//获取VelocityContext
VelocityContext context = new VelocityContext();
//为Context设置变量
context.put("title", "HelloWorld");
context.put("author", "arthinking");
//获取模板文件
Template template = Velocity.getTemplate("helloworld.vm");
StringWriter sw = new StringWriter();
//使用模板文件的merge函数合并模板和context提供的变量,输出到StringWriter中
template.merge(context, sw);
sw.flush();
System.out.println(sw.toString());
 

 

编写helloworld.vm模板文件(保存在file.resource.loader.path设置的目录下):

 

${title}
${author}

 

Velocity模版的基本语法:

 

访问对象属性

和使用EL表达式差不多,直接使用”.”导航。
如访问object对象的id属性:${object.id }

 

遍历List集合

 

#foreach($element in $list)
	#element
#end

 

使用判断语句

 

#if($condition)
	true
#else
	false
#end
 

获取迭代索引值

默认使用变量名:$velocityCount
也可以自定义此变量名,在velocity.properties中设置

 

directive.foreach.counter.name=index

 设置索引起始位置为0

 

directive.foreach.counter.initial.value=0

 

遍历Map变量

 

#foreach($key in $map.keySet())
	$key : $map.get($key)
#end

 

在模板中进行赋值

 

#set(#a=”Hello World!”)
$a

#set($array1=[1..10])
#foreach($entry in $array1)
	#entry
#end
 

使用Velocity模板引擎生成文件

 

//初始化模板引擎
Velocity.init("src/velocity.properties");
//获取VelocityContext
VelocityContext context = new VelocityContext();
//为Context设置变量
context.put("content", "HelloWorld");
context.put("who", "arthinking");
//获取模板文件
Template template = Velocity.getTemplate("helloworld.vm");
//创建输出文件
File output = new File("D:/Velocity/1.html");
if(!output.getParentFile().exists())
	output.getParentFile().mkdir();
//创建输出流
FileOutputStream outputStream = new FileOutputStream(output);
OutputStreamWriter writer = new OutputStreamWriter(outputStream);
BufferedWriter bufferedWriter = new BufferedWriter(writer);
template.merge(context, bufferedWriter);

bufferedWriter.flush();
outputStream.close();
bufferedWriter.close();

 

 

转自:http://www.itzhai.com/the-introduction-of-the-velocity-template-engine-template-language-syntax-and-basic-use.html

分享到:
评论

相关推荐

    Java进阶教程Velocity快速掌握模板引擎视频

    本课程从velocity engine也就是velocity引擎开始, 先讲解velocity的基本使用以及基础语法 , 然后再讲解velocity 的进阶内容velocity Tools , 以及velocity作为web项目的视图改如何使用 , 每一部分都会有一个综合案例...

    generator-velocity:使用 Java Velocity 模板搭建前端 Web 应用程序

    Java Velocity 项目生成器使用 Jave Velocity 模板构建前端 Web 应用程序的生成器。特征CSS 自动前缀带有 LiveReload 的内置预览服务器自动编译 CoffeeScript & Sass 自动 lint 你的脚本自动将图像内联到 css 文件...

    velocity:A node velocity template engine. Node 版 velocity 模板引擎

    速度 节点速度模板引擎。 Node Edition速度模板引擎。0.特点完全实现了速度语法。 查看模板依赖性。 从模板中提取数据结构。 生成中间模板以转储上下文。1.分期付款$ npm install velocity -g2.快速入门一些示例已为...

    velocity入门使用教程

    本人写的velocity教程,包括模板引擎基本使用方法,velocity语法,servlet+velocity例子,spring mvc+velocity例子

    velocity-web.zip

    velocity入门例子-velocity入门demo,给自己备份的

    velocity介绍

    Velocity是一个基于java的模板引擎(template engine)。它允许任何人仅仅简单的使用模板语言(template language)来引用由java代码定义的对象。 当Velocity应用于web开发时,界面设计人员可以和java程序开发人员...

    Freemark基础入门+深入

    Freemaker是一个强大的模板引擎,相比velocity而言,其强大的过程调用、递归和闭包回调功能让freemaker可以完成几乎所有我们所想的功能。

    xmljava系统源码-zhihu:模仿知乎做的SpringBoot问答社交网站项目,项目技术点SpringBoot+Mybatis+Mysq

    进行Spring语言的介绍和讲解,模板语法和渲染(注意springboot1.5开始不支持velocity模板引擎,可以用Thymeleaf或者Freemarker代替) 数据库交互iBatis集成 主要是进行数据库的创建以及交互,讲解注释和XML定义并...

    springboot学习

    chapter3-1-4:使用Velocity模板引擎渲染web视图 chapter3-1-5:使用Swagger2构建RESTful API chapter3-1-6:统一异常处理 chapter3-1-7:使用Java 8中LocalDate等时间日期类的问题解决 chapter3-1-8:扩展XML请求和...

    单点登录源码

    Velocity | 模板引擎 | [http://velocity.apache.org/](http://velocity.apache.org/) ZooKeeper | 分布式协调服务 | [http://zookeeper.apache.org/](http://zookeeper.apache.org/) Dubbo | 分布式服务框架 | ...

    Freemarker使用手册、api、中文版教程

    模板引擎(如Velocity)的工作机理。 注:由于原文档部分内容直译可能难于被读者理解,所以有些地方采用意译为主,因此在翻译用词上 难 免 可 能 会 有 出 入 , 大 家 对 翻 译 的 内 容 有 任 何 意 见 都 可 以...

    springboot参考指南

    模板引擎 vi. 26.1.6. 错误处理 vii. 26.1.7. Spring HATEOAS ii. 26.2. JAX-RS和Jersey iii. 26.3. 内嵌servlet容器支持 i. 26.3.1. Servlets和Filters ii. 26.3.2. EmbeddedWebApplicationContext iii. 26.3.3. ...

Global site tag (gtag.js) - Google Analytics