Velocity是一个基于java的模板引擎。它容许任何人使用简单但功能强大的模板语言引用Java代码中定义的对象。java
当Velocity用于web开发时,web设计人员能够与Java程序员并行工做,根据模型-视图-控制器(MVC)模型开发web站点,这意味着web页面设计人员能够只专一于建立看起来不错的站点,而程序员能够只专一于编写一流的代码。Velocity将Java代码从web页面中分离出来,使web站点在其生命周期内更具可维护性,并为Java服务器页面(jsp)或PHP提供了一个可行的替代方案。程序员
Velocity的功能远远超出了web的范围;例如,它能够用来从模板生成SQL、PostScript和XML。它既能够做为生成源代码和报告的独立实用程序使用,也能够做为其余系统的集成组件使用。例如,Velocity为各类web框架提供模板服务,使它们可以使用视图引擎,以便根据真正的MVC模型开发web应用程序。web
在 Velocity 中全部的关键字都是以#开头的,而全部的变量则是以$开头。Velocity 的语法相似于 JSP 中的 JSTL,甚至能够定义相似于函数的宏,下面来看看具体的语法规则。apache
和咱们所熟知的其余编程语言同样,Velocity 也能够在模板文件中有变量的概念。编程
#set($name =“velocity”)
api
等号后面的字符串 Velocity 引擎将从新解析,例如出现以$开始的字符串时,将作变量的替换。服务器
#set($hello =“hello $name”)
app
上面的这个等式将会给 $hello 赋值为“hello velocity”框架
在模板文件中使用$name 或者 ${name} 来使用定义的变量。推荐使用 ${name} 这种格式,由于在模板中同时可能定义了相似$name 和 $names 的两个变量,若是不选用大括号的话,引擎就没有办法正确识别 $names 这个变量。jsp
#set($name =“ricky”) Welcome $name to velocity\.com
在 Velocity 中循环语句的语法结构以下:
#foreach($element in $list) This is $element $velocityCount #end
Velocity 引擎会将 list 中的值循环赋给 element 变量,同时会建立一个 $velocityCount 的变量做为计数,从 1 开始,每次循环都会加 1
条件语句的语法以下:
#if(condition) #elseif(condition) #else #end
Velocity 引擎提供了 AND、OR 和 NOT 操做符,分别对应&&、||和! 例如:
#if($foo && $bar) #end
#macro(macroName arg1 arg2 …) #end
#macroName(arg1 arg2 …)
#macro(sayHello $name) hello $name #end #sayHello(“velocity”)
输出的结果为 hello velocity
#parse 和 #include 指令的功能都是在外部引用文件,而二者的区别是:#parse 会将引用的内容当成相似于源码文件,会将内容在引入的地方进行解析,#include 是将引入文件当成资源文件,会将引入内容原封不动地以文本输出。分别看如下例子:
foo.vm 文件:
#set($name =“velocity”)
parse.vm:
#parse(“foo.vm”)
输出结果为:velocity
include.vm:
#include(“foo.vm”)
输出结果为:#set($name =“velocity”)
package com.bytebeats.velocity.sample; import java.io.StringWriter; import org.apache.velocity.VelocityContext; import org.apache.velocity.Template; import org.apache.velocity.app.Velocity; import org.apache.velocity.exception.ResourceNotFoundException; import org.apache.velocity.exception.ParseErrorException; import org.apache.velocity.exception.MethodInvocationException; public class SingletonModelDemo { public static void main(String[] args) { Velocity.init(); VelocityContext context = new VelocityContext(); context.put("name", "Velocity"); Template template = null; try { template = Velocity.getTemplate("mytemplate.vm"); } catch( ResourceNotFoundException e ) { // couldn't find the template } catch( ParseErrorException pee ) { // syntax error: problem parsing the template } catch( MethodInvocationException mie ) { // something invoked in the template // threw an exception } catch( Exception e ) { } StringWriter sw = new StringWriter(); template.merge(context, sw); System.out.println("content:"+sw.toString()); } }
package com.bytebeats.velocity.sample; import org.apache.velocity.Template; import org.apache.velocity.VelocityContext; import org.apache.velocity.app.VelocityEngine; import java.io.StringWriter; public class SeparateInstanceDemo { public static void main(String[] args) { //1. create a new instance of the engine VelocityEngine ve = new VelocityEngine(); //2. configure the engine ve.setProperty(VelocityEngine.RESOURCE_LOADER, "classpath"); //3. initialize the engine ve.init(); VelocityContext context = new VelocityContext(); context.put("name", "Velocity"); Template template = ve.getTemplate("foo.vm"); StringWriter sw = new StringWriter(); template.merge(context, sw); System.out.println("content:"+sw.toString()); } }
还有另一种方式来配置:
Properties props = new Properties(); props.load(this.getClass().getResourceAsStream("/vm.properties")); VelocityEngine ve = new VelocityEngine(props); ve.init();