`

velocity 学习中

 
阅读更多

Velocity的运转流程:
Velocity就是一个template引擎,或是代码生成器。
templateenginecontext组成。
1. 首先创建一个template(如果是用在web上就是一个html文件),将需要参数化或实例化的地方用跟context有关的符号标记出来,标记时使用velocity template language。而template应该可以是任意的文本。
2. 给context设定一些值,这些值用来替换在template中被标记的地方。
3. 利用engine将template中需要替换的地方用context中的值替换掉,也就是所谓的merge,从而得到该模板的实例。
除此之外,还有些组件:
logging:用log4j作为其日志系统。
Resource loader:控制网页生成中使用的模板。
Anakia:一个示例应用,该应用允许不使用xsl处理xml。
Application servers:对所有主流的servers和servlet提供了支持,比如有一个
VelocityServlet类。
Velocity应该包括下面三个方面的内容:
1.Velocity Template Language

主要有三种元素:
1. reference:
三种:
variable:$variable,
properties:$identifier.propertyName
methods:$identifier.methodIdentifier<parameter>
properties和methods都是从context中定义的object来获取的。

variable:
variable指向java对象,并利用其toString()值来显示。
因此,虽然java对象都有toString,但是默认的该方法通常没有意义,除了包装类,像Integer,Float。所以,如果要使一般对象的toString给出有意义的结果就要重写相应对象的toString方法。

method:
任何插入到template或从template中读取的value都被当作string处理。非string的数据会被隐式地转换为string。但是当从template中定义的method引用中读取参数时,情况不是这么简单。
Velocity引擎希望所有从template中读取的参数都是string,当使用string做参数的时候,在template中要用双引号或单引号标识。同时支持将integer类型的数据以literal的方式定义(不带引号)。超出integer范围的数字,或其他类型的数字(比如浮点数)是不支持的。 如果要使用这样不被支持的数据类型,只能在java端做显式的数据转换工作。也许以后的velocity可支持更多类型的数据类型。
下面是template中使用string和integer数据的例子:
Defining automobile color...$car.defineColor( "Blue" )
Defining automobile year...$car.defineYear( 1997 )

property:
Property是method的一个扩展。比如对于$obj.Value,velocity就会从obj中寻找名为getValue()的方法,如果不存在,寻找getvalue()。应该指出,property的使用虽然不是大小写敏感,但是跟大小写有关系。对于上例,如果改成$.value,那么velocity先寻找方法getvalue(),然后才是getValue()。以上都是利用java的自省机制introspection来实现的。如下面这个例子,展示了大小写问题在template中的应用:
$car.Color $car.Year $car.Type
$car.color $car.year $car.type
$car.getColor() $car.getYear() $car.getType()
三行的结果一样:
还可以定义类似
Object get(Object key)
String get(String key)
的方法,
当getValue和getvalue都不存在的时候,会转向get方法,针对上面的例子,这个get方法可以这样定义:
public String get( String item )
{
if ( item.equalsIgnoreCase( "type" ) )
{
return (make + " " + model);
}
else
if ( item.equalsIgnoreCase( "color" ) )
{
return (color);
}
else
if ( item.equalsIgnoreCase( "year" ) )
{
return (year.toString());
}
else
{
return ("");
}
}
2. directive:构成template中的控制结构
#stop:停止对template的处理
#include:
#parse:
#set:直接影响与该template关联的context
结果:
#end:与#if, #foreach, or #macro一起使用,标志结束。
#if:
#else
#elseif
#foreach:提供一种能力,可以遍历一组项目a list of items,并对每个项目做
出处理。
自定义被遍历的项目:
从变量,方法,hash表,Vector,链表中获取被遍历的项目:
#macro:模板重用技术
3. velocimacros,
2.Velocity context
3.taking control of velocity

运行时的配置runtime configuration
[1]默认方式:Velocity.init();
[2]利用配置文件:
配置文件:
## Specify the names of our custom libraries
velocimacro.library = tags.vm, labels.vm
## Disable inline Velocimacro definitions
velocimacro.permissions.allow.inline = false
## Enable Velocimacro library auto-reloading
velocimacro.library.autoreload = true
相关代码:
// Initialize template engine
try
{
Velocity.init( "custom.properties" );
}
catch( Exception x )
{
System.err.println( "Failed to initialize Velocity: " + x );
System.exit( 1 );
}
[3]利用Properties对象,建立属性。
Properties customProps = new Properties();
// Specify the names of our custom libraries
customProps.setProperty( "velocimacro.library", "tags.vm, labels.vm" );
// Disable inline Velocimacro definitions
customProps.setProperty( "velocimacro.permissions.allow.inline", "false" );
// Enable Velocimacro library auto-reloading
customProps.setProperty( "velocimacro.library.autoreload", "true" );
// Initialize template engine
try
{
Velocity.init( customProps );
}
catch( Exception x )
{
System.err.println( "Failed to initialize Velocity: " + x );
System.exit( 1 );
}
[4]直接利用Velocity.setProperty
// Specify the names of our custom libraries
Velocity.setProperty( "velocimacro.library", "tags.vm, labels.vm" );
// Disable inline Velocimacro definitions
Velocity.setProperty( "velocimacro.permissions.allow.inline", "false" );
// Enable Velocimacro library auto-reloading
Velocity.setProperty( "velocimacro.library.autoreload", "true" );
// Initialize template engine
try
{
Velocity.init();
}
catch( Exception x )
{
System.err.println( "Failed to initialize Velocity: " + x );
System.exit( 1 );
}
运行时配置的属性分6
[1]directive,影响velocity指令的行为
[2]encoding,template的编码和跟velocity template engine相关的工具的数据。
[3]logging,影响velocity使用的日志系统的行为。
[4]resource management,velocity的资源管理的行为。
资源就是template engine的输入,resource loader就是知道resource位置的实体entity。有几种resource loader:File,JAR,Classpath,DataaSource,分别有相应的类去实现,比如FileResourceLoader,JarResourceLoader等。
[5]miscellaneous,影响velocity的各种行为。
[6]velocimacros
事件:
为了更好控制template processing,velocity提供有限的基于事件处理的用户交互。有三种事件:
[1]试图通过#set指令,将null赋值给一个velocity引用时,NullSetEventHandler,
[2]java方法试图调用velocity方法或属性引用抛出异常时,MethodExceptionEventHandler
[3]跟velocity引用有关的value被插入到输出流output stream的时候,ReferenceInsertionEventHandler
多个context构成的context chaining:
在一个应用中可以使用一个context,也可以使用多个context,多个context间会有wrap关系。
比如,在层次化的数据中data layering,先创建一个context包含核心数据,然后利用这个context,创建另一个context包含附加数据,这个context还可以继续被用来创建新的context。这样做的情况是:从独立的数据集中创建一个聚集的数据集aggregate data set,后面的数据会覆盖前面的数据。另一个情况是:核心数据将保存一个template,但是对不同的template processing需要在该template基础上做些修改。
空格的处理
的输出:
可以看到template中的空格影响了最后的输出结果,这是要注意的。
Singleton和非singleton的velocity engine:
非singleton的模式允许多个velocity engine同时存在于jvm中。多个velocity engine对需要多个运行时配置的情况有用。
Velocityserlvet
velocity用作MVC中的v,而javaweb下可以用servletc,其他的servletjavabeanm:这就是一个web应用程序的解决方案。
Velocity内置了对serlvet的支持:
使用特殊的servlet:VelocityServlet,
velocity中支持的对servlet的操作:
方法:
handleRequest:
以request,response,context为参数,返回template。
两个任务:
利用request参给servlet的参数,以及response,设置context。
读取template。
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics