首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

【转】Velocity研究学习文档

Posted on 2012-03-22 00:38  饭后爱  阅读(506)  评论(0编辑  收藏  举报

1、概述:
a) Velocity是 一个基于java的模板引擎,简称VTL。
b) 主要目的是弥补JSP的不足,把页 面设计从繁重的java编码中解脱出来。
c) 使用新颖的语法格式,简洁、高效。
2、基本概念
a) Velocity的 语法虽然不完全类似于java但也基于面向对象的语法规范。
b) 基本语言元素和控制语句如附录A。
i. ‘#’所有的Velocity语句都是由它开始,结尾不需要特殊符号;
例如:
#if( $foo == $bar )
#if( $foo > 42 )
#if( $foo < 42 )
ii. ‘$’所有的变量,属性,对象的使用都有它开始;
例如:
$mud-Slinger_9
$!mud-Slinger_9
${mud-Slinger_9}
iii. 变量;
1. 正则表达式:
$ [ ! ][ { }[ a..z, A..Z ][ a..z, A..Z, 0..9, -, _ ][ ] ]
例如:
Normal notation: $mud-Slinger_9
Silent notation: $!mud-Slinger_9
Formal notation: ${mud-Slinger_9}
2. 变量分为三种基本形式:
普通型(Normal notation),这是Velocity中最常用的变 量类型,可适用于绝大多数情况;
哑元型(Silent notation),主要用在当你使用一个变量,可是它 还没有值时,哑元型会 为你输出一个空字符串;
正式型(Formal notation), 这种类型主要用载需要区分变量名称时,如使用变量$test但在页面中后面紧跟着页面内容,如:$testHello,变量会被误认为 是$testHello,所以改变如下:${test}Hello,即可正常使用;
3. 变量的注释:
在Velocity中‘$’是保 留字,所以页面中对它的使用就会受到一定的限制,如下的页面内容:
$2003year,是不会有问题的,因为在Velocity中变量必须使用字 母开头(’!’、’{‘除外}所以这个字串会被正常显示;
$year2003,出现这样的字串,分两种情况:1.但当前并没有定义这个变量名,系 统一样会正常显示;换言之如果要使用变量但没有 定义系统都会将其当作一般字符串打印出来;2. 当前已定义这个变量名,则系统会把所有出现的此字串当作变量处理,为避免这种情况,Velocity给出了特殊符号修饰符’\’,假设给此变量赋值 good year,结果如下:
$year2003 good year
\$year2003 $year2003
\\$year2003 \good year
\\\$year2003 \$year2003
以此类推。
注:单独使用变量,如:<input type="text" name="email" value="$email"/>,不算完整语句,不需要在行首加’#’
iv. 属 性:
$ [ { }[ a..z, A..Z ][ a..z, A..Z, 0..9, -, _ ]* .[a..z, A..Z ][ a..z, A-Z, 0..9, -, _ ]* [ ] ]
3、例子
a) 一个参数设置的例子,
<html>
<body>HELLO $CUSTOMERNAME</body>
</html>
我们可以在运行时得到客户的名字,然后把它插入到这个模版中替换变量$CUSTOMERNAME,整个替换过程是由Velocity进行控制的,而且java的调用代码也非常简单,如我们可以在java代码中这样 调用
/***********************************************************/
//这个文件中设定了Velocity使 用的log4j的配置和Velocity的模版文件所在的目录
Velocity.init("D:\\Template\\resource\\jt.properties");
//模版文件名,模版文件所在的路径在上一条语句中已经设置了
Template template = Velocity.getTemplate("hello.vm", "gb2312");
//实例化一个Context
VelocityContext context = new VelocityContext();
//把模版变量的值设置到context中
context.put("CUSTOMERNAME", "My First Template Engine ---- Velocity.");
//开始模版的替换
template.merge(context, writer);
//写到文件中
PrintWriter filewriter = new PrintWriter(new FileOutputStream(outpath),true);
filewriter.println(writer.toString());
filewriter.close();
b) 操作对象的一个例子
<html>
<body>
Name:$Customer.Name()
Address:$Customer.Address()
Age:$Customer.Age()
</body>
</html>

java的代码:
/***********************************************************/
//设置客户信息
Customer mycustomer = new Customer();
mycustomer.setName("Velocity");
mycustomer.setAddress("jakarta.apache.org/velocity/index.html");
mycustomer.setAge(2);
//这个文件中设定了 Velocity 使 用的 Log4j 的配置和Velocity的模 版文件所在的目录Velocity.init("D:\\Template\\resource\\jt.properties");
//模版文件名,模版文件所在的路径在上一条语句中已经设置了
Template template = Velocity.getTemplate("hello.vm", "gb2312");
//实例化一个Context
VelocityContext context = new VelocityContext();
//把模版变量的值设置到context中
context.put("Customer", mycustomer);
//开始模版的替换
template.merge(context, writer);
//写到文件中
PrintWriter filewriter = new PrintWriter(new FileOutputStream(outpath),true);
filewriter.println(writer.toString());
filewriter.close();
输出结果:
<html>
<body>
Name:Velocity
Address:jakarta.apache.org/velocity/index.html
Age:2
</body>
</html>
4、与JSP的比较
Velocity vs. JSP: 两者都是实现同一功能的template languages. JSP 是 Sun 的标准,Velocity是 Open Source 开发的系统。在Open Source 看 来,JSP 是 Sun 开发的一个"不好用"的产品。"不好用"主要体现在: 难于Debug; 没有必要地把简单问题复杂化, 如JSP的先编译再运行;不 能防止用户将Java code 嵌入JSP, 从而破坏MVC的设计. 并且Sun 将JSP冠之以"Standard",有“胁天子以令诸侯”之嫌。当然,Struts和Tag Lib将Java code 从JSP中剥离,挽救了JSP.