天天看點

Struts旅程(五)struts控制器DispatchAction

       上篇示範了struts架構的由來,進而展現struts架構優點。Struts中的表單處理器為ActionForm,而struts中的控

s制器主要是Action,以及DispatchAction控制器等。

       Action

       在struts中,所有的使用者都會經過ActionServlet的處理,而實際的工作是交給Action對象來處理的,

ActionServlet可以從配置檔案中建立ActionMapping對象,從ActionMapping對象中找到對應使用的Action,然

後将使用者請求轉交給Action。

       對Struts一個ActionMapping隻能生成一個Action對象,當使用者發起請求的時候會檢查所需要的Action對象是

否存在,如果不存在就會生成一個Action對象,在以後的處理過程中使用這個對象。

       當我們使用Action的時候需要繼承arg.apache.struts.action.Action這個類,在子類中加入所需要的業務邏輯處

理,這些子類會傳回ActionForward對象,ActionServlet接受這個對象,把頁面轉發到指定頁面,進而把使用者請求的

結果發送到對應的頁面。我們在struts-config.xml中進行配置。配置的主要屬性如下:

      (1)  path屬性:通路Action的URL位址,當使用者請求路徑和URL比對時,ActionServlet會把這個使用者請求發送

給Action處理。

      (2)  type屬性:指定處理請求的Action對應的類,需要寫類檔案的包路徑。

      (3)  name屬性:指定我們的Action用到的ActionForm名字,這個ActionForm必須是在<form-beans>中定

義過的。

     (4)  scope屬性:指定ActionForm的使用範圍,預設值為session範圍。

     (5)  input屬性:指定表單驗證出錯的時候轉向頁面。

     (6)  validate屬性:指明是否自動調用ActionForm中的validate方法對表單進行驗證。

      配置示例如下代碼。

[html] view plain copy print?

Struts旅程(五)struts控制器DispatchAction
Struts旅程(五)struts控制器DispatchAction
  1. <struts-config>  
  2.      <form-beans>  
  3.             <form-bean name="loginForm" type="com.bjpowernode.struts.LoginActionForm" />  
  4.      </form-beans>  
  5.      <action-mappings>  
  6.             <action path="/login"  
  7.                           type="com.bjpowernode.struts.LoginAction"  
  8.                           name="loginForm"  
  9.                           scope="request"  
  10.                           >  
  11.                           <forward name="success" path="/login_success.jsp"/>  
  12.                           <forward name="error" path="/login_error.jsp"/>  
  13.             </action>  
  14.      </action-mappings>  
  15. </struts-config>  

        問題

       當我們完成使用者增删改查操作時采用struts架構,我們需要為增删改查建立四個不同的Action,如果有更多的增

删改查操作,比如對物料增删改查也需要建立四個Action,這樣造成了大量的Action。

       問題的解決

       在struts中的Action類中,隻提供了一個execute()方法,一個使用者請求URL隻能對應一個servlet,在struts中

提供了另一個控制器類org.apache.struts.actions.DispatchAction,這個類可以經完成相關業務邏輯所需方法幾種在

一個DispatchAction類中,我們繼承DispatchAction類後不重寫execute()方法,而是編寫自己的方法,在不同的

方法中處理不同的動作。删除使用者增删改查對應的Action,建立UserAction(如圖5.1)。

Struts旅程(五)struts控制器DispatchAction

圖5.1

        界面中調用代碼如下所示。

[html] view plain copy print?

Struts旅程(五)struts控制器DispatchAction
Struts旅程(五)struts控制器DispatchAction
  1. <body>  
  2. <a href="user/user_maint.do?command=list"title="請點選通路使用者管理系統">使用者管理系統</a>  
  3. </body>  

        其中list對應着UserAction中的list方法,傳遞的字元串與UserAction中的方法名相同。

        UserAction中的代碼如下所示:

[java] view plain copy print?

Struts旅程(五)struts控制器DispatchAction
Struts旅程(五)struts控制器DispatchAction
  1. packagecom.bjpowernode.drp.web.actions;  
  2. importjava.util.Date;  
  3. importjava.util.List;  
  4. importjavax.servlet.http.HttpServletRequest;  
  5. importjavax.servlet.http.HttpServletResponse;  
  6. importorg.apache.commons.beanutils.BeanUtils;  
  7. importorg.apache.struts.action.ActionForm;  
  8. importorg.apache.struts.action.ActionForward;  
  9. importorg.apache.struts.action.ActionMapping;  
  10. importorg.apache.struts.actions.DispatchAction;  
  11. importcom.bjpowernode.drp.manager.UserManager;  
  12. importcom.bjpowernode.drp.model.User;  
  13. importcom.bjpowernode.drp.web.forms.UserActionForm;  
  14. public classUserAction extends DispatchAction {  
  15.      protected ActionForward list(ActionMapping mapping, ActionForm form,  
  16.                    HttpServletRequestrequest, HttpServletResponse response)  
  17.                    throwsException {  
  18.             //調用業務邏輯操作  
  19.             List userList = UserManager.getInstance().findAllUserList();  
  20.             request.setAttribute("userlist",userList);  
  21.             returnmapping.findForward("list_success");  
  22.      }  
  23.      public ActionForward del(ActionMapping mapping, ActionForm form,  
  24.                    HttpServletRequestrequest, HttpServletResponse response)  
  25.                    throws Exception {  
  26.             //擷取從頁面表單中送出過來的值  
  27.             UserActionForm uaf = (UserActionForm)form;  
  28.             //取得需要删除的userId的集合  
  29.             String[] userIdList = uaf.getSelectFlag();  
  30.             //調用業務邏輯操作  
  31.             UserManager.getInstance().deleteUsers(userIdList);  
  32.             return mapping.findForward("del_success");  
  33.      }  
  34.      public ActionForward add(ActionMapping mapping, ActionForm form,  
  35.                    HttpServletRequest request, HttpServletResponse response)  
  36.                    throwsException {  
  37.             //擷取從頁面表單中送出過來的值  
  38.             UserActionForm uaf = (UserActionForm)form;  
  39.             Useruser = new User();  
  40.             BeanUtils.copyProperties(user,uaf);  
  41.             user.setCreateDate(newDate());  
  42.             //調用業務邏輯操作  
  43.             UserManager.getInstance().addUser(user);  
  44.             returnmapping.findForward("add_success");    }  
  45.      public ActionForward modify(ActionMapping mapping, ActionForm form,  
  46.                    HttpServletRequestrequest, HttpServletResponse response)  
  47.                    throwsException {  
  48.             //擷取從頁面表單中送出過來的值  
  49.             UserActionForm uaf = (UserActionForm)form;  
  50.             User user = new User();  
  51.             BeanUtils.copyProperties(user,uaf);  
  52.             //調用業務邏輯操作  
  53.             UserManager.getInstance().modifyUser(user);  
  54.             returnmapping.findForward("modify_success");  
  55.      }  
  56.      public ActionForward find(ActionMapping mapping, ActionForm form,  
  57.                    HttpServletRequestrequest, HttpServletResponse response)  
  58.                    throwsException {  
  59.             //擷取從頁面表單中送出過來的值  
  60.             UserActionForm uaf = (UserActionForm)form;  
  61.             String userId = uaf.getUserId();  
  62.             //調用業務邏輯操作  
  63.             User user = UserManager.getInstance().findUserById(userId);  
  64.             //将user對象從Action傳遞到JSP頁面  
  65.             request.setAttribute("user",user);  
  66.             returnmapping.findForward("find_success");  
  67.      }  
  68. }  

         Struts-config.xml配置檔案代碼如下所示。

[html] view plain copy print?

Struts旅程(五)struts控制器DispatchAction
Struts旅程(五)struts控制器DispatchAction
  1. <struts-config>  
  2.     <form-beans>  
  3.             <form-bean name="userForm"type="com.bjpowernode.drp.web.forms.UserActionForm"/>  
  4.     </form-beans>  
  5.     <action-mappings>  
  6.         <action path="/user/user_maint"  
  7.                       type="com.bjpowernode.drp.web.actions.UserAction"  
  8.                       name="userForm"  
  9.                       scope="request"  
  10.                       parameter="command"  
  11.         >  
  12.                <forward name="list_success" path="/user/user_list.jsp"/>  
  13.                <forward name="del_success" path="/user/user_maint.do?command=list"redirect="true"/>  
  14.                <forward name="add_success"  path="/user/user_maint.do?command=list"redirect="true"/>  
  15.                <forward name="modify_success" path="/user/user_maint.do?command=list"redirect="true"/>  
  16.                <forward name="find_success"  path="/user/user_modify.jsp"/>  
  17.   </action-mappings>    
  18. </struts-config>  

        其中配置Action的時候,配置了parameter屬性,并且指定了parameter屬性值為command,當使用者單擊添加

或删除使用者操作時,會以http://localhost:8080/struts_dispatchaction_usermgr/user/user_maint.do?

command=list,這個請求會被映射到UserAction控制器中,Struts根據method參數的值把這個請求發送到控制器

UserAction的list方法。這樣取得參數完成頁面的調用。

       從上述可以看出,DispatchAction可以通過command這個參數的值來決定調用DispatchAction的哪個方法,

DispatchAction是從使用者請求的URL中提取parameter定義參數的值,進而決定調用哪個方法處理使用者請求。是以

DispatchAction不能通過ActionForm向伺服器送出請求,因為送出表單的時候不能向伺服器傳遞參數。

       根據上述示例我們可以總結出DispatchAction與Action差別:Action是從表單中取得資料,并且自動轉換為對

應的類型。而DispatchAction取得配置檔案中parameter,截取parameter定義的參數值。但是DispatchAction可

以處理多個動作而不需要建立多個Action。

       DispatchAction可以在同一個控制器中處理多個動作,但隻能通過URL來調用控制器,根據使用者送出的參數來決

定調用哪個方法來處理使用者請求。不能通過表單送出使用者請求資訊,在struts中如果要在同一個表單中處理不同的動

作,可以使用LookupDispatchAction。在這裡就不詳細講述了,有興許的童鞋可以查找些資料來實作。

       下一篇struts頁面轉發控制ActionForward和ActionMapping。

繼續閱讀