<rt id="bn8ez"></rt>
<label id="bn8ez"></label>

  • <span id="bn8ez"></span>

    <label id="bn8ez"><meter id="bn8ez"></meter></label>

    Junky's IT Notebook

    統(tǒng)計(jì)

    留言簿(8)

    積分與排名

    WebSphere Studio

    閱讀排行榜

    評(píng)論排行榜

    WebWork2教程(中文版)(4.2)

    4.2、在WebWork中使用Velocity

    使用Velocity作為視圖,有兩種方法:

    l         使用velocity結(jié)果類型來(lái)呈現(xiàn)Velocity模板

    l         web.xml中注冊(cè)WebWorkVelocityServlet,直接請(qǐng)求Velocity模板文件來(lái)呈現(xiàn);這種方法要在web.xml中為WebWorkVelocityServlet添加一個(gè)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結(jié)果類型意味著Velocity模板是通過(guò)Action來(lái)呈現(xiàn)。如果訪問(wèn).vm文件,不會(huì)呈現(xiàn)該文件,而是返回文本文件。因此,Velocity模板文件應(yīng)該放在WEB-INF目錄下,使其無(wú)法直接訪問(wèn)。

    使用WebWorkVelocityServlet意味著可以通過(guò)請(qǐng)求.vm文件來(lái)呈現(xiàn)Velocity模板,這可能需要在模板中實(shí)現(xiàn)安全檢查。

    無(wú)論使用哪種方法,在寫模板時(shí),Velocity的所有特性都是有效的,并且有一些WebWork的特定功能可以使用。這里假設(shè)你對(duì)Velocity很熟悉,重點(diǎn)放在WebWork的特定功能上。

    1WebWork的特定功能

    WebWorkValue Stack中提供了一些可以訪問(wèn)的對(duì)象,包括:

    l         當(dāng)前的HttpServletRequest

    l         當(dāng)前的HttpServletResponse

    l         當(dāng)前的OgnlValueStack

    l         OgnlTool實(shí)例

    l         當(dāng)前Action類的所有屬性

    要訪問(wèn)Value Stack中的對(duì)象,需要在模板中正確使用Velocity引用:

    l         $req=HttpServletRequest

    l         $res=HttpServletResponse

    l         $stack=OgnlValueStack

    l         $ognl=OgnlTool

    l         $name-of-property=當(dāng)前Action類的屬性

    2)使用velocity結(jié)果類型

    下面的例子是使用Velocity模板作為結(jié)果,來(lái)實(shí)現(xiàn)前面的Hello例子,注意<property value="person" />標(biāo)記被$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標(biāo)記

    使用Velocity模板替代JSP標(biāo)記,會(huì)失去使用JSP標(biāo)記的能力。然而,WebWorkVelocity Servlet提供了一種在Velocity模板中使用JSP標(biāo)記的方法:使用#tag#bodytag#param Velocimacros。下面是通用語(yǔ)法:

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

    或:

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

    下面的例子使用Velocity實(shí)現(xiàn)4.1.1節(jié)中示范UI標(biāo)記用法的例子:

    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節(jié)的例子)

    FormProcessingAction-validation.xml:(同4.1.1節(jié)的例子)

    下面的例子使用Velocity實(shí)現(xiàn)4.1.1節(jié)中自定義組件的例子,注意#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節(jié)的例子)

     

    4.3、在WebWork中使用Freemaker(略)

    posted on 2007-06-28 09:39 junky 閱讀(793) 評(píng)論(0)  編輯  收藏 所屬分類: web

    主站蜘蛛池模板: 日韩精品视频免费在线观看| 最近免费字幕中文大全| 美女网站免费福利视频| 亚洲精品国产情侣av在线| 最近2019免费中文字幕6| 伊人久久综在合线亚洲2019| 无码精品人妻一区二区三区免费看| 亚洲精品夜夜夜妓女网| 成人久久免费网站| 久久久久亚洲精品无码蜜桃| 亚洲高清视频免费| 在线精品亚洲一区二区 | 三根一起会坏掉的好痛免费三级全黄的视频在线观看| 日韩免费电影在线观看| 国产精品久久久久久亚洲小说| 亚洲av无码乱码在线观看野外| 一级A毛片免费观看久久精品| 国产精品亚洲二区在线观看 | 亚洲美女中文字幕| 91在线品视觉盛宴免费| 亚洲人成欧美中文字幕| 又爽又黄无遮挡高清免费视频 | 亚洲美日韩Av中文字幕无码久久久妻妇| 国产亚洲精品欧洲在线观看| 亚洲精品老司机在线观看| 国产性生大片免费观看性| 亚洲色欲www综合网| 日韩在线天堂免费观看| 国产成人高清精品免费观看| 亚洲成年轻人电影网站www| 亚色九九九全国免费视频| 曰批免费视频播放免费| 久久久久亚洲AV无码专区首| 免费在线观看的网站| 污污视频网站免费观看| 噜噜噜亚洲色成人网站∨| 国产真实伦在线视频免费观看| 国产精品免费大片一区二区| 亚洲一级黄色大片| 国产午夜亚洲不卡| 欧美三级在线电影免费|