上篇演示了[struts](http://blog.csdn.net/lovesummerforever/article/details/18963959)[框架的由來](http://blog.csdn.net/lovesummerforever/article/details/18963959),從而體現struts框架優點。Struts中的表單處理器為ActionForm,而struts中的控制器主要是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方法對表單進行驗證。
配置示例如下代碼。
~~~
<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
界面中調用代碼如下所示。
~~~
<body>
<a href="user/user_maint.do?command=list"title="請點擊訪問用戶管理系統">用戶管理系統</a>
</body>
~~~
其中list對應著UserAction中的list方法,傳遞的字符串與UserAction中的方法名相同。
UserAction中的代碼如下所示:
~~~
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");
}
/**
* 用戶刪除
* @param mapping
* @param form
* @param request
* @param response
* @return
* @throws Exception
*/
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");
}
/**
* 用戶添加
* @param mapping
* @param form
* @param request
* @param response
* @return
* @throws Exception
*/
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"); }
/**
* 修改用戶
* @param mapping
* @param form
* @param request
* @param response
* @return
* @throws Exception
*/
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");
}
/**
* 根據ID查詢用戶
*
* @param mapping
* @param form
* @param request
* @param response
* @return
* @throws Exception
*/
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配置文件代碼如下所示。
~~~
<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](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。
·
- 前言
- Struts旅程(一)Struts簡介和原理
- struts旅程(二)Struts登錄示例
- Struts旅程(三)Struts表單處理器ActionForm(靜態動態)
- Struts旅程(四)MVC向struts MVC框架演變過程
- Struts旅程(五)struts控制器DispatchAction
- Struts旅程(六)Struts頁面轉發控制ActionForward和ActionMapping
- Hibernate旅程(一)Hibernate架構概述
- Hibernate旅程(二)Hibernate實例
- Hibernate旅程(三)Hibernate持久化對象的三個狀態
- Hibernate旅程(四)Hibernate對數據庫刪除、查找、更新操作
- Hibernate旅程(五)Hibernate映射--基本類映射和對象關系映射
- Hibernate旅程(六)Hibernate映射--繼承映射
- Hibernate旅程(七)Hibernate緩存機制--一級緩存
- Hibernate旅程(八)Hibernate緩存機制--二級緩存
- Hibernate旅程(九)Hibernate緩存機制--查詢緩存
- Spring旅程(一)為什么使用Spring
- Spring旅程(二)非Spring向Spring過渡-- Spring IOC容器的使用
- Spring旅程(三) AOP--Spring AOP容器基礎
- Spring旅程(四) AOP--Spring AOP實例
- SSH旅程(五)Spring運用到Hibernate中
- SSH旅程(六)Spring和struts結合(方案一)