Junky's IT Notebook

          統計

          留言簿(8)

          積分與排名

          WebSphere Studio

          閱讀排行榜

          評論排行榜

          WebWork2教程(中文版)(3)

          3ActionsResults

          Actions是基本執行單元,在WebWork配置中注冊,用來響應特定的請求。在MVC中,Actions是控制部分。下面是在WebWork中創建Action的基本步驟:

          l         創建調用ActionJSP頁;

          l         創建Action類;

          l         創建處理Action返回結果的JSP頁;

          l         xwork.xml中注冊Action

          1Hello WebWorld的例子

          l         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 03: HelloWebWorldAction. --> 
                        <action name="helloWebWorld" class="lesson03.HelloWebWorldAction"> 
                          <result name="success" type="dispatcher">ex01-success.jsp</result> 
                        </action> 
                 </package>
          </xwork>

          配置文件告訴WebWork,有一個叫helloWebWorldAction,由lesson03.HelloWebWorldAction實現;同時定義了一個叫success的結果,指向ex01-success.jsp頁面;

          l         調用Action的頁面ex01-index.jsp

          <html>
          <head>
          <title>WebWork Tutorial - Lesson 3 - Example 1</title>
          </head>
           
          <body>
           
          <p>Click the button below to activate HelloWebWorldAction.</p>
           
          <form action="helloWebWorld.action" method="post">
          <p><input type="submit" value="Hello!" /></p>
          </form>
           
          </body>
          </html>

          當點擊頁面的按鈕時,瀏覽器提交表單數據給helloWebWorld.action;既然URL匹配映射*.actionServlet容器激活WebWorkServletDispatcherServletDispatcher讀取xwork.xml,查找名為helloWebWorldAction,如果找到就創建Action類的一個新實例,調用execute()方法

          l         Action類:HelloWebWorldAction.java

          package lesson03; 
           
          import com.opensymphony.xwork.ActionSupport; 
           
          public class HelloWebWorldAction extends ActionSupport { 
                 String hello; 
                 public String getHello() { 
                        return hello; 
                 }
                 public String execute() throws Exception { 
                        hello = "Hello, WebWorld!"; 
                        return SUCCESS; 
                 } 
          }

          Action類繼承com.opensymphony.xwork.ActionSupport,并實現execute()方法;execute()方法的返回值SUCCESSsuccess<result>name屬性值)對應;ServletDispatcher查找名字相匹配的result,轉移到指定的JSPex01-success.jsp

          l         結果顯示JSPex01-success.jsp

          <%@ taglib uri="webwork" prefix="ww" %> 
          <html> 
          <head> 
          <title>WebWork Tutorial - Lesson 3 - Example 1</title> 
          </head> 
          <body> 
           
          <ww:property value="hello" /> 
           
          </body> 
          </html>

          <ww:property value="hello" />Action類中查找hello屬性,調用hello屬性的setter方法獲得屬性值(在execute()中已經設置),顯示Hello, WebWorld!

          2)向Action提供數據的例子

          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 03: HelloAction. -->
                        <action name="hello" class="lesson03.HelloAction">
                          <result name="error" type="dispatcher">ex02-index.jsp</result>
                          <result name="success" type="dispatcher">ex02-success.jsp</result>
                        </action>
                 </package>
          </xwork>

          HelloAction.java

          package lesson03;
           
          import com.opensymphony.xwork.ActionSupport;
           
          public class HelloAction extends ActionSupport {
                 String person;
                 public String getPerson() {
                        return person;
                 }
                 public void setPerson(String person) {
                        this.person = person;
                 }
                 public String execute() throws Exception {
                        if ((person == null) || (person.length() == 0)) return ERROR;
                        else return SUCCESS;
                 }
          }

          ex02-index.jsp

          <html>
          <head>
          <title>WebWork Tutorial - Lesson 3 - Example 2</title>
          </head>
           
          <body>
           
          <p>What's your name?</p>
           
          <form action="hello.action" method="post">
          <p><input type="text" name="person" /><input type="submit" /></p>
          </form>
           
          </body>
          </html>

          ex02-success.jsp

          <%@ taglib uri="webwork" prefix="ww" %>
          <html>
          <head>
          <title>WebWork Tutorial - Lesson 3 - Example 2</title>
          </head>
          <body>
           
          Hello, <ww:property value="person" />
           
          </body>
          </html>

          這個例子使用POST方法向Action發送表單數據(使用person名),在HelloAction的實例創建以后,ServletDispatcher調用setter方法用數據設置Action的對應屬性(person);因此,在執行execute()之前,person屬性已經設置了

          3result的類型

          上面的例子中使用的result類型是dispatcher,而WebWorkresult類型已經在webwork-default.xml中配置:

          l         dispatcher (com.opensymphony.webwork.dispatcher.ServletDispatcherResult) forwards 結果到指定位置URL

          l         redirect (com.opensymphony.webwork.dispatcher.ServletRedirectResult) redirects結果到指定位置URL ;與dispatcher不同的是不會發送表單數據

          l         velocity (com.opensymphony.webwork.dispatcher.VelocityResult:使用 Velocity 模版作為結果,需要在web.xml中配置 VelocityServlet,這是一種很好的方法

          l         chain (com.opensymphony.xwork.ActionChainResult): Action鏈,將結果傳送給另外一個Action

          l         xslt (com.opensymphony.webwork.views.xslt.XSLTResult): 使用XSLT式樣格式化結果

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

          主站蜘蛛池模板: 平原县| 大厂| 大石桥市| 山阴县| 河西区| 东宁县| 黎平县| 玛沁县| 义马市| 贵州省| 浠水县| 凤山市| 澄迈县| 藁城市| 资源县| 容城县| 河南省| 怀集县| 沁阳市| 甘洛县| 桃园县| 青浦区| 舞钢市| 北流市| 通道| 广平县| 克拉玛依市| 玛纳斯县| 合山市| 察隅县| 湄潭县| 竹山县| 东安县| 磐安县| 肇州县| 武功县| 古丈县| 科技| 塘沽区| 湟源县| 诸城市|