Velocity用户指南(中文版)(1)

来源:互联网 发布:常德市优化办工作人员 编辑:程序博客网 时间:2024/05/17 18:12
<script type="text/javascript"><!--google_ad_client = "pub-2947489232296736";/* 728x15, 创建于 08-4-23MSDN */google_ad_slot = "3624277373";google_ad_width = 728;google_ad_height = 15;//--></script><script type="text/javascript"src="http://pagead2.googlesyndication.com/pagead/show_ads.js"></script>
<script type="text/javascript"><!--google_ad_client = "pub-2947489232296736";/* 160x600, 创建于 08-4-23MSDN */google_ad_slot = "4367022601";google_ad_width = 160;google_ad_height = 600;//--></script><script type="text/javascript"src="http://pagead2.googlesyndication.com/pagead/show_ads.js"></script>

什么是Velocity

Velocity是基于Java的模板引擎。它允许Web页面开发者引用Java代码中定义的方法。Web设计者可以和Java程序开发者并行开发遵循MVC模式的Web站点。这意味着,Web设计者可以将精力放在好的Web站点设计上,而Java程序开发者可以将精力放在编写代码上。VelocityJava代码从Web页面中分离,使Web站点更具长期可维护性,并提供了一种替代JSPPHP的方案。

 

VTL(Velocity Template Language)介绍

VTL提供一种简单、容易和干静的方法将动态内容合并到Web页面。VTL使用引用(references)将动态内容插入到Web页面中。变量是一种引用,可以指向Java代码中的定义内容,或者由Web页面中的VTL语句来获得值。下面是一个可以插入到HTML文档的VTL语句的例子:

#set( $a = "Velocity" )

VTL语句以#开头,并包含指令(set)。变量以$开头,用引号引起。引号可以是单引号,也可以是双引号。前者引用具体的String值;后者可以包含Velocity引用,例如”hello, $name” $name会用其当前的值替换。上面的例子是将值Velocity赋值给变量a

当变量被赋值后,就可以在HTML文档的任何地方引用,下面是Hello Velocity World!的例子:

#set( $foo = "Velocity" )
Hello $foo World!

 

注释

VTL支持单行注释(以##开始)和多行注释(包括在#**#之间),下面是一个例子:

This text is visible. ## This text is not visible.
This text is visible.
This text is visible. #* This text, as part of a multi-line comment,
is not visible. This text is not visible; it is also part of the
multi-line comment. This text still not visible. *# This text is outside
the comment, so it is visible.
## This text is not visible.

 

<script type="text/javascript"><!--google_ad_client = "pub-2947489232296736";/* 728x15, 创建于 08-4-23MSDN */google_ad_slot = "3624277373";google_ad_width = 728;google_ad_height = 15;//--></script><script type="text/javascript"src="http://pagead2.googlesyndication.com/pagead/show_ads.js"></script>
<script type="text/javascript"><!--google_ad_client = "pub-2947489232296736";/* 160x600, 创建于 08-4-23MSDN */google_ad_slot = "4367022601";google_ad_width = 160;google_ad_height = 600;//--></script><script type="text/javascript"src="http://pagead2.googlesyndication.com/pagead/show_ads.js"></script>
原创粉丝点击