`
george.gu
  • 浏览: 73296 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

Velocity Usage

阅读更多

 

You can find velocity mannual from http://velocity.apache.org/engine/devel/user-guide.html.

Here is summarize on how to use velocity.

Velocity Usage:

Velocity is a Java-based template engine. 

We can use velocity to define web page instead of JSP, there are several advantages:

 

  1. It permits web page designers to reference methods defined in Java code. Web designers can work in parallel with Java programmers to develop web sites according to the Model-View-Controller (MVC) model, meaning that web page designers can focus solely on creating a well-designed site, and programmers can focus solely on writing top-notch code. 
  2. Simplify web page logical and make it more easy to be managed by web-designer
  3. Web page written in velocity template can be updated without re-deploy you web application.

 

We can also use velocity to generate other output based on template:

 

  1. XML
  2. SQL
  3. Some other file template with dynamic data by defining them in velocity syntax.

 

Variable Definition:

 

The shorthand notation of a variable consists of a leading "$" character followed by a VTL Identifier. A VTL Identifier must start with an alphabetic character (a .. z or A .. Z). The rest of the characters are limited to the following types of characters:

 

  • alphabetic (a .. z, A .. Z)
  • numeric (0 .. 9)
  • hyphen ("-")
  • underscore ("_")

Following definition have different meaning:

$count:

Usage: #set($count=10)

define a variable named "count"

$count-1:

Usage: #set($count-1=10)

define a variable named "count-1". Warning: it is not a evaluation to get the value of $count minus 1.

$count -1:

Usage: #set($count=$count -1)

Error!

Exception : org.apache.velocity.exception.ParseErrorException: Encountered "-1" at...

$count - 1:

Usage: #set($count=$count - 1)

execute an evaluation which get result of $count minus 1.

has same value as "$count + -1"

${count}-1:

Usage: #set($count=${count}-1)

Error!

Get the same error as "$count -1". 

${count}- 1:

Usage: #set($count=${count}- 1)

execute an evaluation which get result of $count minus 1.

has same value as "${count}+ -1"

 

Exception

But if you want to display "10-1" in your output, you can use following template:

#set( $count=10)

${count}-1

#[[

$count-1 will lead to "$count-1", because there is no variable named "count-1";

$count -1 will lead to "10 -1" which has one more space than "10-1".

$!count-1 will lead to " ", because there is no variable named "count-1";

]]#


 

Quiet Reference Notation "!" and conditional logical NOT "!"

When Velocity encounters an undefined reference, its normal behavior is to output the image of the reference. For example, suppose the following reference appears as part of a VTL template.

my email is "$email"

In case there is no variable defined for $email, it will display: my email is "$email".

If you set Quiet Reference Notation as following:

my email is "$!email"

In case there is no variable defined for $email, it will display: my email is "".

But conditional logical NOT is different:

#if(!$email)

I have no email.

#else

my email is "#email"

#end

Note: if only one variable is evaluated to determine whether it is true, like if($email). Which will happen under one of two circumstances: 

 

  1. $email is a boolean (true/false) which has a true value, or 
  2. $email is NOT a boolean and its value is not null.

#include() vs. #parse():

  1. include() can contain a set of files separated by commas, like: include("file1.vm", "file2.vm", ...) But parse() can only contain one file.
  2. Velocity does not render variables inside included files, but the variables inside parse file will be rendered.
  3. You can use variable to replace filename in both.

Properties reference to Variable

Only references to the attribute equivalents of JavaBean getter/setter methods are resolved (i.e. $foo.Name does resolve to the class Foo's getName() instance method, but not to a public Name instance variable of Foo).  
So please make sure your template refer to JavaBean valid getter/setter method.
JavaBean.java as following:

  public class JavaBean {

private String a;
private String b = "Iamb";
public JavaBean() {
}
public String geta() {
return "Iama";
}
public String getA() {
return "IamA";
}
}
Your template test.vm as following:

  $bean.a

$bean.A
$bean.b
Then you set VelocityContext as following:

  VelocityContext context = new VelocityContext();

context.put("bean", new JavaBean());
PrintWriter writer = new PrintWriter(System.out);
Template template = Velocity.getTemplate("test.vm");
template.merge(context, writer);

You will get the output as following:
Iama
IamA
$bean.b

Normally the property lookup rules as following:
  1. geta()
  2. getA()
  3. get("a")
  4. isA()

Directive:

#set:

We can set a variable in velocity with #set directive:
#set($count=10)

literal:

string literals that are enclosed in double quote characters will be parsed and rendered. 
However, when the string literal is enclosed in single quote characters, it will not be parsed

Condition: 

#if(condition1)
...
#elseif(condition2)
...
#else
...
#end 

Loops:

#foreach ($each in $all)
...
#end
you can use #break to stop looping.

velocity.properties:

I would like to discuss velocity.properties and how it control default velocity render behavior.
To be updated later.

 

分享到:
评论

相关推荐

    mat-velocity:提供velocity模板的渲染功能

    Usage var mat = require('mat') var velocity = require('mat-velocity') var rewrite = require('mat-rewrite') mat.task('daily', function () { mat.url([/\.vm/]) .use(velocity({ })) }) api 用来处理vm的...

    mybatis-generator-usage

    标题 "mybatis-generator-usage" 暗示我们要探讨的是关于MyBatis Generator的使用方法。MyBatis Generator是一款强大的工具,它能够自动生成MyBatis的映射文件、Java模型类以及DAO接口,极大地提高了开发效率。接...

    node-soft-mirror:使用节点的 Velocity 软镜像以毫秒为单位启动

    ##Usage 在你的框架 package.js 中引用这个包,然后在服务器上使用类似的东西: Meteor . call ( 'velocity/mirrors/request' , { framework : 'myFramework' } ) ; VelocityMirrors . find ( { framework : '...

    amazing:Velocity 前端工程问题集成解决方案

    amazing: Velocity 前端工程问题集成解决方案 Usage 全局安装amazing npm install -g amazing 到web工程的根目录下 cd /(webapp) 初始化本地服务器web工程加载目录 amazing -i 启动服务器 amazing Others 自定义端口...

    Android代码-IntelliJDashPlugin

    A smart and simple plugin that provides keyboard shortcut access for Dash, Velocity or Zeal in IntelliJ IDEA, RubyMine, WebStorm, PhpStorm, PyCharm, DataGrip, CLion, GoLand and Android Studio. ...

    SSH学习小结SSH学习小结

    Struts2还集成了许多其他框架,如Freemarker和Velocity模板引擎,以及iBatis和Hibernate持久化库。 **Hibernate框架** 是一个流行的Java ORM(对象关系映射)工具,它允许开发者用Java对象来操作数据库,减少了编写...

    NovodeX Physics SDK Documentation

    While this documentation will cover the features and usage of the SDK, because of the academically challenging nature of the subject matter, it is assumed that you have mastered the material covered ...

    大数据与绿色数据中心26.pptx

    - 绿色数据中心旨在提高能源效率,降低PUE(Power Usage Effectiveness),采用分布式技术、X86平台和本地存储,以减少能耗和环境影响。 - RDBMS架构与Hadoop架构的对比,揭示了如何通过计算与存储的融合和横向...

    大数据与绿色数据中心.pptx

    绿色数据中心则关注能源效率,衡量标准是PUE(Power Usage Effectiveness),即数据中心总能耗与IT设备能耗之比。降低PUE意味着更高的能源效率,更少的环境影响。绿色数据中心通过优化冷却系统、使用节能设备和实施...

    patcher:修补 Meteor 方法并允许您出于测试目的覆盖“this”属性

    您的支持帮助我们继续我们在 Velocity 和相关框架方面的工作。 #安装 meteor add xolvio:patcher#Usage 这个包设置了debugOnly标志,这意味着它不会被捆绑到应用程序中。 这也意味着您只能将其作为弱依赖引用,...

    大数据时代与数据中心建设(推荐文档).docx

    大数据的特点包括大量性(Volume)、高速性(Velocity)、多样性和价值密度低(Variety),以及真实性(Veracity)。大数据的出现对企业的决策制定、业务模式和创新能力产生了深远影响。 【数据中心建设】 数据中心...

    abaqus关键字中文

    understanding the usage of ABAQUS keywords is essential for modifying .inp files and performing finite element analysis. *AMPLITUDE:幅值关键字,用于定义幅值曲线的名称、类型和参数。该关键字在定义...

    云计算与大数据技术应用习题.pdf

    PUE(Power Usage Effectiveness)是衡量数据中心能源效率的指标,定义为数据中心整体能耗与IT设备能耗之比。而DCIE(Data Center Infrastructure Efficiency)是数据中心基础设施效率,表示数据中心用于运行IT设备...

Global site tag (gtag.js) - Google Analytics