隨筆-23  評論-0  文章-5  trackbacks-0
            
          初學struts也是一知半解,比如action與dispatchAction的區別,最近便讓我十分困惑。  

          做為一個初學者,在目前使用過的東西中,主要有mapping.findForward(),action,dispatchAction三個實用功能,這三者放在一起本不太合適,但是在我看來,也有一定的相關性。  

          struts主要用途
          1. 判斷url傳值所要做的操作。比如 http://www.baidu.com/index.jsp?run=showAll ,這個如果使用了action,可以使用request.getParameter()接收。
          2. 使用mapping.findForward(url);替換servlet的response.sendRedirect("http://www.baidu.com");
          3. 把form提交的內容封裝到formBean。這樣在使用了大量form的時候,可以用formBean的一實例lf.formName這樣去調用。
          好處在于不用使用request.getParameter("formName");去接收。
          4. 使用dispatchAction,在struts-config.xml中進行配置,直接可以判斷get鏈接中的傳值,也可以避免使用request.getParameter("formName");去接收。

          formBean與dispatchAction的區別: 
          顯而易見,formBean使用在大量的post表單的情況下。
          dispatchAction常用來處理url鏈接中傳來的值。

          Action與dispatchAction的區別: 
          這里使用區別并不合適,因為dispatchAction繼承自Action,所以dispatchAction是對action進行了功能的擴充,action常需要使用getParameter()先獲取傳入的值,再判斷這個值是否正常,再重定向到不同的頁面。 
          而dispatchAction把判斷放以了struts-config.xml文件中,而不需要再到業務層進行判斷。并且dispatchAction在使用中,更常用于處理url傳入的get請求。


          Struts的生存周期

          啟動web服務器 -- 加載web.xml文件 -- 產生ActionServlet實例 -- 加載struts-config.xml文件 -- 解析出多個action并放入actionMapping池 
          客戶端發現請求 -- web容器 -- 請求發送到ActionServlet -- 分發到不同的action并實例化 -- 模型層處理 -- 處理結果返回到actionServlet -- 返回結果到視圖層

          舉例
          下面是一個使用了dispatchAction的項目的完整struts-config.xml配置
          ---------------------------------------------
          <?xml version="1.0" encoding="UTF-8"?>
          <!DOCTYPE struts-config PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 1.2//EN" "http://struts.apache.org/dtds/struts-config_1_2.dtd">

          <struts-config>
          <data-sources />
          <form-beans />
          <global-exceptions />
          <global-forwards />
          <form-beans>      //formbean的定義,name="loginForm"對應于下文中的name="loginForm"
          <form-bean name="loginForm" type="org.jsw.struts.form.LoginForm" />
          </form-beans>
          <action-mappings >

          <action                    //定義了一個action
          path="/manager"           //此action的訪問路徑
          name="loginForm"          //此action用于接收哪個formbean,對應于上文<form-beans>中的內容
          parameter="method"      //此action用于接怍method的值,如果method值是add,那么自動調用業務層的add方法
          type="com.umt.struts.action.ManagerAction">
          <forward name="success" path="/success.jsp"></forward>     //定義了重向向地址
          <forward name="delete" path="/delete.jsp"></forward>
          </action>
          </action-mappings>

          <message-resources parameter="com.jsw.struts.ApplicationResources" />
          </struts-config>
          posted on 2013-11-05 16:37 ForMeBlog 閱讀(479) 評論(0)  編輯  收藏 所屬分類: 框架集合
          主站蜘蛛池模板: 同德县| 东阳市| 龙泉市| 岫岩| 抚松县| 罗源县| 塔河县| 桓仁| 洪泽县| 图木舒克市| 抚远县| 贡觉县| 陵川县| 名山县| 尼勒克县| 高要市| 宁河县| 沾化县| 青神县| 石河子市| 建始县| 桑日县| 丰都县| 青海省| 张家口市| 镶黄旗| 海淀区| 农安县| 额济纳旗| 宝坻区| 赤水市| 五大连池市| 黑河市| 宾川县| 广元市| 大兴区| 长汀县| 苏尼特左旗| 余干县| 酉阳| 理塘县|