随笔-31  评论-2  文章-0  trackbacks-0

基本语法

声明

#set ($var=XXX)
左边可以是以下的内容:
  • Variable reference
  • String literal
  • Property reference
  • Method reference
  • Number literal #set ($i=1)
  • ArrayList #set ($arr=["yt1","t2"])
  • 算术运算符
  • References 引用的类型

注释

单行:

## this is a comment

多行:

#* this line
and this line
and this line, are comments...*#

变量 Variables

以 "$" 开头,第一个字符必须为字母。character followed by a VTL Identifier. (a .. z or A .. Z).
变量可以包含的字符有以下内容:
  • alphabetic (a .. z, A .. Z)
  • numeric (0 .. 9)
  • hyphen ("-")
  • underscore ("_")

Properties

$Identifier.Identifier
$user.name
hashtable user中的的name值.类似:user.get("name")

h2、Methods

object user.getName() = $user.getName()

h2、Formal Reference Notation

用{}把变量名跟字符串分开。如

#set ($user="csy"}
${user}name

返回csyname

$与$!的区别

当找不到username的时候,$username返回字符串"$username",而$!username返回空字符串""

双引号 与 引号

#set ($var="helo")

则 test"$var" 返回testhello,test'$var' 返回test'$var'。
可以通过设置 stringliterals.interpolate=false改变默认处理方式

条件语句

#if( $foo )
<strong>Velocity!</strong>
#end
#if($foo)
#elseif()
#else
#end

当$foo为null或为Boolean对象的false值执行.

逻辑运算符:

== && || !

循环语句

#foreach($var in $arrays ) // 集合包含下面三种Vector, a Hashtable or an Array
#end

#foreach( $product in $allProducts )
<li>$product</li>
#end

#foreach( $key in $allProducts.keySet() )
<li>Key: $key -> Value: $allProducts.get($key)</li>
#end

#foreach( $customer in $customerList )
<tr><td>$velocityCount</td><td>$customer.Name</td></tr>
#end

velocityCount变量在配置文件中定义

  1. Default name of the loop counter
  2. variable reference.
    directive.foreach.counter.name = velocityCount
  3. Default starting value of the loop
  4. counter variable reference.
    directive.foreach.counter.initial.value = 1

包含文件

#include( "one.gif","two.txt","three.htm" )

Parse导入脚本

#parse("me.vm" )

#stop 停止执行并返回

定义宏

Velocimacros, 相当于函数支持包含功能:

#macro( d )
<tr><td></td></tr>
#end
调用
#d()

带参数的宏

#macro( tablerows $color $somelist )
#foreach( $something in $somelist )
<tr><td bgcolor=$color>$something</td></tr>
#end
#end

Range Operator

#foreach( $foo in [1..5] )

与struts2的整合

模板装载位置(按顺序依次搜索)

  • Web application 应用程序路径,会覆盖掉类路径下的同名配置文件;
  • Class path 类路径,一般为缺省模板的配置,公共模板位置;

数据来源(按顺序依次搜索)

  • The value stack
  • The action context
  • Built-in variables

Struts2-Velocity集成的一些隐含变量

  • stack - ValueStack自身。调用方式:${stack.findString('ognl expr')}
  • action - 最新操作的action
  • reponse
  • request
  • session
  • applicaion - 获得servlet的环境
  • base - 请求环境的路径

Velocity Result 输出模板

模拟jsp执行环境,使用velocity的模板直接显示到servelet的输出流。

location - 模板位置,没有其它参数时的缺省配置。
parse - 默认true ,false将不解析Ognl expressions.

配置范例:

<result name="success" type="velocity">
<param name="location">foo.vm</param>
</result>

等价于以下的配置方式:

<result name="success" type="velocity">
foo.vm
</result>

Velocity语法

http://blog.csdn.net/alexwan/archive/2007/10/29/1853285.aspx

Struts 与 Velocity 的集成

http://www.ibm.com/developerworks/cn/java/j-sr1.html

posted on 2009-07-02 09:38 xiaoxinchen 阅读(148) 评论(0)  编辑  收藏

只有注册用户登录后才能发表评论。


网站导航: