在WebWork中使用Velocity

4.2、在WebWork中使用Velocity

使用Velocity作为视图,有两种方法:

l         使用velocity结果类型来呈现Velocity模板

l         web.xml中注册WebWorkVelocityServlet,直接请求Velocity模板文件来呈现;这种方法要在web.xml中为WebWorkVelocityServlet添加一个Servlet映射,如下:

<servlet> 
     
     
       <servlet-name>velocity</servlet-name> 
     
     
       <servlet-class>com.opensymphony.webwork.views.velocity.WebWorkVelocityServlet</servlet-class> 
     
     
       <load-on-startup>1</load-on-startup> 
     
     
</servlet> 
     
     
<servlet-mapping> 
     
     
       <servlet-name>velocity</servlet-name> 
     
     
       <url-pattern>*.vm</url-pattern> 
     
     
</servlet-mapping>
     
     

使用velocity结果类型意味着Velocity模板是通过Action来呈现。如果访问.vm文件,不会呈现该文件,而是返回文本文件。因此,Velocity模板文件应该放在WEB-INF目录下,使其无法直接访问。

使用WebWorkVelocityServlet意味着可以通过请求.vm文件来呈现Velocity模板,这可能需要在模板中实现安全检查。

无论使用哪种方法,在写模板时,Velocity的所有特性都是有效的,并且有一些WebWork的特定功能可以使用。这里假设你对Velocity很熟悉,重点放在WebWork的特定功能上。

1WebWork的特定功能

WebWorkValue Stack中提供了一些可以访问的对象,包括:

l         当前的HttpServletRequest

l         当前的HttpServletResponse

l         当前的OgnlValueStack

l         OgnlTool实例

l         当前Action类的所有属性

要访问Value Stack中的对象,需要在模板中正确使用Velocity引用:

l         $req=HttpServletRequest

l         $res=HttpServletResponse

l         $stack=OgnlValueStack

l         $ognl=OgnlTool

l         $name-of-property=当前Action类的属性

2)使用velocity结果类型

下面的例子是使用Velocity模板作为结果,来实现前面的Hello例子,注意<property value="person" />标记被$person引用所替代:

xwork.xml

<!DOCTYPE xwork PUBLIC "-//OpenSymphony Group//XWork 1.0//EN" 
     
     
"http://www.opensymphony.com/xwork/xwork-1.0.dtd">
     
     
<xwork>
     
     
       <!-- Include webwork defaults (from WebWork-2.1 JAR). -->
     
     
       <include file="webwork-default.xml" />
     
     
       <!-- Configuration for the default package. -->
     
     
       <package name="default" extends="webwork-default">
     
     
              <!-- Default interceptor stack. --> 
     
     
              <default-interceptor-ref name="defaultStack" /> 
     
     
              <!-- Action: Lesson 4.2: HelloAction using Velocity as result. -->
     
     
              <action name="helloVelocity" class="lesson03.HelloAction">
     
     
                <result name="error" type="dispatcher">ex01-index.jsp</result>
     
     
                <result name="success" type="velocity">ex01-success.vm</result>
     
     
              </action>
     
     
       </package>
     
     
</xwork>
     
     

HelloAction.java:(同前面的例子)

ex01-index.jsp:(同前面的例子)

ex01-success.vm

<html> 
     
     
<head> 
     
     
<title>WebWork Tutorial - Lesson 4.2 - Example 1</title> 
     
     
</head> 
     
     
<body> 
     
     
Hello, $person 
     
     
</body> 
     
     
</html>
     
     

3)在Velocity中使用WebWork标记

使用Velocity模板替代JSP标记,会失去使用JSP标记的能力。然而,WebWorkVelocity Servlet提供了一种在Velocity模板中使用JSP标记的方法:使用#tag#bodytag#param Velocimacros。下面是通用语法:

#tag (name-of-tag list-of-attributes)
     
     

或:

#bodytag (name-of-tag list-of-attributes)
     
     
       #param (key value)
     
     
       #param (key value)
     
     
...
     
     
#end
     
     

下面的例子使用Velocity实现4.1.1节中示范UI标记用法的例子:

xwork.xml

<!DOCTYPE xwork PUBLIC "-//OpenSymphony Group//XWork 1.0//EN" 
     
     
"http://www.opensymphony.com/xwork/xwork-1.0.dtd">
     
     
<xwork>
     
     
       <!-- Include webwork defaults (from WebWork-2.1 JAR). -->
     
     
       <include file="webwork-default.xml" />
     
     
       <!-- Configuration for the default package. -->
     
     
       <package name="default" extends="webwork-default">
     
     
              <!-- Default interceptor stack. --> 
     
     
              <default-interceptor-ref name="defaultStack" /> 
     
     
              <!-- Actions: Lesson 4.2: FormProcessingAction using Velocity. --> 
     
     
              <action name="formProcessingVelocityIndex" class="lesson04_02.FormProcessingIndexAction">
     
     
                <result name="success" type="velocity">ex02-index.vm</result>
     
     
              </action>
     
     
              <action name="formProcessingVelocity" class="lesson04_01_01.FormProcessingAction"> 
     
     
                <result name="input" type="velocity">ex02-index.vm</result> 
     
     
                <result name="success" type="velocity">ex02-success.vm</result> 
     
     
                <interceptor-ref name="validationWorkflowStack" /> 
     
     
              </action> 
     
     
       </package>
     
     
</xwork>
     
     

ex02-index.vm

<html> 
     
     
<head> 
     
     
<title>WebWork Tutorial - Lesson 4.2 - Example 2</title> 
     
     
<style type="text/css"> 
     
     
  .errorMessage { color: red; } 
     
     
</style>   
     
     
</head> 
     
     
<body> 
     
     
<p>UI Form Tags Example using Velocity:</p> 
     
     
#bodytag (Form "action='formProcessingVelocity.action'" "method='post'") 
     
     
       #tag (Checkbox "name='checkbox'" "label='A checkbox'" "fieldValue='checkbox_value'") 
     
     
       #tag (File "name='file'" "label='A file field'") 
     
     
       #tag (Hidden "name='hidden'" "value='hidden_value'") 
     
     
       #tag (Label "label='A label'") 
     
     
       #tag (Password "name='password'" "label='A password field'") 
     
     
       #tag (Radio "name='radio'" "label='Radio buttons'" "list={'One', 'Two', 'Three'}") 
     
     
       #tag (Select "name='select'" "label='A select list'" "list={'One', 'Two', 'Three'}" 
     
     
              "emptyOption=true") 
     
     
       #tag (Textarea "name='textarea'" "label='A text area'" "rows='3'" "cols='40'") 
     
     
       #tag (TextField "name='textfield'" "label='A text field'") 
     
     
       #tag (Submit "value='Send Form'") 
     
     
#end 
     
     
</body> 
     
     
</html>
     
     

ex02-success.vm

<html> 
     
     
<head> 
     
     
<title>WebWork Tutorial Lesson 4.2 - Example 2</title> 
     
     
</head> 
     
     
<body> 
     
     
<p>UI Form Tags Example result using Velocity:</p> 
     
     
<ul> 
     
     
       <li>checkbox: $!checkbox</li> 
     
     
       <li>file: $!file</li> 
     
     
       <li>hidden: $!hidden</li> 
     
     
       <li>password: $!password</li> 
     
     
       <li>radio: $!radio</li> 
     
     
       <li>select: $!select</li> 
     
     
       <li>textarea: $!textarea</li> 
     
     
       <li>textfield: $!textfield</li> 
     
     
</ul> 
     
     
</body> 
     
     
</html>
     
     

FormProcessingAction.java:(同4.1.1节的例子)

FormProcessingAction-validation.xml:(同4.1.1节的例子)

下面的例子使用Velocity实现4.1.1节中自定义组件的例子,注意#param的用法:

ex03.vm

<html> 
     
     
<head> 
     
     
<title>WebWork Tutorial - Lesson 4.2 - Example 3</title> 
     
     
</head> 
     
     
<body> 
     
     
<p>Custom Component Example:</p> 
     
     
<p> 
     
     
#bodytag (Component "template=/files/templates/components/datefield.vm") 
     
     
       #param ("label" "Date") 
     
     
       #param ("name" "mydatefield") 
     
     
       #param ("size" "3") 
     
     
#end 
     
     
</p> 
     
     
</body> 
     
     
</html>
     
     

/files/templates/components/datefield.vm(同4.1.1节的例子)

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值