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

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

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

    Junky's IT Notebook

    統計

    留言簿(8)

    積分與排名

    WebSphere Studio

    閱讀排行榜

    評論排行榜

    WebWork2教程(中文版)(4.2)

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

     

    4.3、在WebWork中使用Freemaker(略)

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

    主站蜘蛛池模板: 久久精品亚洲精品国产色婷| 国产精品小视频免费无限app| 91av免费在线视频| 亚洲国产精品久久网午夜| 亚洲精品国产精品乱码不卡√| 日韩免费视频一区| 18级成人毛片免费观看| 中国在线观看免费的www| 久久亚洲精品11p| 亚洲日韩在线中文字幕第一页| 国产卡二卡三卡四卡免费网址| 日本一区二区免费看| 又大又硬又粗又黄的视频免费看| 亚洲日本成本人观看| 亚洲午夜电影在线观看高清 | 久久亚洲精品成人777大小说| 亚洲AV无码乱码精品国产| 成人影片麻豆国产影片免费观看| 7x7x7x免费在线观看| 中文字幕无码一区二区免费| 亚洲综合激情视频| 亚洲成A人片777777| 亚洲色婷婷六月亚洲婷婷6月| 高清一区二区三区免费视频| 国产精品永久免费视频| 特级毛片aaaa级毛片免费| 亚洲狠狠婷婷综合久久| 亚洲色偷精品一区二区三区| 亚洲13又紧又嫩又水多| 狠狠色香婷婷久久亚洲精品| 亚洲伊人色一综合网| 77777午夜亚洲| 亚洲 日韩 色 图网站| 爱情岛论坛网亚洲品质自拍| 亚洲第一页综合图片自拍| 四虎免费影院4hu永久免费| 四虎国产精品免费视| 亚洲精品A在线观看| 91麻豆精品国产自产在线观看亚洲 | 1000部啪啪毛片免费看| 99久热只有精品视频免费观看17|