上篇示範了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?
![](https://img.laitimes.com/img/_0nNw4CM6IyYiwiM6ICdiwiIn5Gcu82Yp9VRE90Qvw1c0V2czF2LcRXZu5ibkN3YuUGZvN2Lc9CX6MHc0RHaiojIsJye.png)
- <struts-config>
- <form-beans>
- <form-bean name="loginForm" type="com.bjpowernode.struts.LoginActionForm" />
- </form-beans>
- <action-mappings>
- <action path="/login"
- type="com.bjpowernode.struts.LoginAction"
- name="loginForm"
- scope="request"
- >
- <forward name="success" path="/login_success.jsp"/>
- <forward name="error" path="/login_error.jsp"/>
- </action>
- </action-mappings>
- </struts-config>
問題
當我們完成使用者增删改查操作時采用struts架構,我們需要為增删改查建立四個不同的Action,如果有更多的增
删改查操作,比如對物料增删改查也需要建立四個Action,這樣造成了大量的Action。
問題的解決
在struts中的Action類中,隻提供了一個execute()方法,一個使用者請求URL隻能對應一個servlet,在struts中
提供了另一個控制器類org.apache.struts.actions.DispatchAction,這個類可以經完成相關業務邏輯所需方法幾種在
一個DispatchAction類中,我們繼承DispatchAction類後不重寫execute()方法,而是編寫自己的方法,在不同的
方法中處理不同的動作。删除使用者增删改查對應的Action,建立UserAction(如圖5.1)。
圖5.1
界面中調用代碼如下所示。
[html] view plain copy print?
![](https://img.laitimes.com/img/_0nNw4CM6IyYiwiM6ICdiwiIn5Gcu82Yp9VRE90Qvw1c0V2czF2LcRXZu5ibkN3YuUGZvN2Lc9CX6MHc0RHaiojIsJye.png)
- <body>
- <a href="user/user_maint.do?command=list"title="請點選通路使用者管理系統">使用者管理系統</a>
- </body>
其中list對應着UserAction中的list方法,傳遞的字元串與UserAction中的方法名相同。
UserAction中的代碼如下所示:
[java] view plain copy print?
![](https://img.laitimes.com/img/_0nNw4CM6IyYiwiM6ICdiwiIn5Gcu82Yp9VRE90Qvw1c0V2czF2LcRXZu5ibkN3YuUGZvN2Lc9CX6MHc0RHaiojIsJye.png)
- packagecom.bjpowernode.drp.web.actions;
- importjava.util.Date;
- importjava.util.List;
- importjavax.servlet.http.HttpServletRequest;
- importjavax.servlet.http.HttpServletResponse;
- importorg.apache.commons.beanutils.BeanUtils;
- importorg.apache.struts.action.ActionForm;
- importorg.apache.struts.action.ActionForward;
- importorg.apache.struts.action.ActionMapping;
- importorg.apache.struts.actions.DispatchAction;
- importcom.bjpowernode.drp.manager.UserManager;
- importcom.bjpowernode.drp.model.User;
- importcom.bjpowernode.drp.web.forms.UserActionForm;
- public classUserAction extends DispatchAction {
- protected ActionForward list(ActionMapping mapping, ActionForm form,
- HttpServletRequestrequest, HttpServletResponse response)
- throwsException {
- //調用業務邏輯操作
- List userList = UserManager.getInstance().findAllUserList();
- request.setAttribute("userlist",userList);
- returnmapping.findForward("list_success");
- }
- public ActionForward del(ActionMapping mapping, ActionForm form,
- HttpServletRequestrequest, HttpServletResponse response)
- throws Exception {
- //擷取從頁面表單中送出過來的值
- UserActionForm uaf = (UserActionForm)form;
- //取得需要删除的userId的集合
- String[] userIdList = uaf.getSelectFlag();
- //調用業務邏輯操作
- UserManager.getInstance().deleteUsers(userIdList);
- return mapping.findForward("del_success");
- }
- public ActionForward add(ActionMapping mapping, ActionForm form,
- HttpServletRequest request, HttpServletResponse response)
- throwsException {
- //擷取從頁面表單中送出過來的值
- UserActionForm uaf = (UserActionForm)form;
- Useruser = new User();
- BeanUtils.copyProperties(user,uaf);
- user.setCreateDate(newDate());
- //調用業務邏輯操作
- UserManager.getInstance().addUser(user);
- returnmapping.findForward("add_success"); }
- public ActionForward modify(ActionMapping mapping, ActionForm form,
- HttpServletRequestrequest, HttpServletResponse response)
- throwsException {
- //擷取從頁面表單中送出過來的值
- UserActionForm uaf = (UserActionForm)form;
- User user = new User();
- BeanUtils.copyProperties(user,uaf);
- //調用業務邏輯操作
- UserManager.getInstance().modifyUser(user);
- returnmapping.findForward("modify_success");
- }
- public ActionForward find(ActionMapping mapping, ActionForm form,
- HttpServletRequestrequest, HttpServletResponse response)
- throwsException {
- //擷取從頁面表單中送出過來的值
- UserActionForm uaf = (UserActionForm)form;
- String userId = uaf.getUserId();
- //調用業務邏輯操作
- User user = UserManager.getInstance().findUserById(userId);
- //将user對象從Action傳遞到JSP頁面
- request.setAttribute("user",user);
- returnmapping.findForward("find_success");
- }
- }
Struts-config.xml配置檔案代碼如下所示。
[html] view plain copy print?
![](https://img.laitimes.com/img/_0nNw4CM6IyYiwiM6ICdiwiIn5Gcu82Yp9VRE90Qvw1c0V2czF2LcRXZu5ibkN3YuUGZvN2Lc9CX6MHc0RHaiojIsJye.png)
- <struts-config>
- <form-beans>
- <form-bean name="userForm"type="com.bjpowernode.drp.web.forms.UserActionForm"/>
- </form-beans>
- <action-mappings>
- <action path="/user/user_maint"
- type="com.bjpowernode.drp.web.actions.UserAction"
- name="userForm"
- scope="request"
- parameter="command"
- >
- <forward name="list_success" path="/user/user_list.jsp"/>
- <forward name="del_success" path="/user/user_maint.do?command=list"redirect="true"/>
- <forward name="add_success" path="/user/user_maint.do?command=list"redirect="true"/>
- <forward name="modify_success" path="/user/user_maint.do?command=list"redirect="true"/>
- <forward name="find_success" path="/user/user_modify.jsp"/>
- </action-mappings>
- </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。