内置的struts Action 类----DispatchAction
                                                            生活随笔
收集整理的這篇文章主要介紹了
                                内置的struts Action 类----DispatchAction
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.                        
                                
                            
                            
                            一、用途:通常在一個Action中只能完成一種業務操作,例如用戶的增、刪、改得放在三個Action 中,但是如果我想把這三個業務都放在一個Action中,想通過URL后跟參數來區分,即通過http://localhost:8080/proj/user.do?method=insert 表示增加
http://localhost:8080/proj/user.do?method=delete 表示刪除
http://localhost:8080/proj/user.do?method=alter 表示修改
可見增、刪、改是寫在 path 為 user的 一個action 這個時候就用到的DispatchAction
二、使用方法
1、創建的 action 繼承 DispatchAction
2、創建自己需要的方法,要和action中的execute有相同的參數個數和參數類型
如下例:一個action中有兩個方法,add與sub, 它們都與execute有相同的參數個數和參數類型
public class Test1Action extends DispatchAction {
public ActionForward sub(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response)
throws Exception {
//SessionContainer a=this.getSessionContainer(request);
System.out.println("+++++++++sub++++++++++++");
return null;
}
public ActionForward add(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response)
throws Exception {
System.out.println("+++++++++++add+++++++++++");
return null;
}
}
 
3、在struts.xml中進行配置
<action path="/test1"
type="com.qf.struts.action.Test1Action"
validate="false"
parameter="method" />
4、http://localhost:8080/test/test1.do?method=sub
http://localhost:8080/test/test1.do?method=add
分別測試
action的parameter屬性決定了URL后跟的參數變量
三、找不到action的檢查方法
1、首先檢查action中的方法是否是那四個參數,以及是否 throws 了Exception
2、檢查action的配置文件是否配置了parameter 屬性。
                        
                        
                        http://localhost:8080/proj/user.do?method=delete 表示刪除
http://localhost:8080/proj/user.do?method=alter 表示修改
可見增、刪、改是寫在 path 為 user的 一個action 這個時候就用到的DispatchAction
二、使用方法
1、創建的 action 繼承 DispatchAction
2、創建自己需要的方法,要和action中的execute有相同的參數個數和參數類型
如下例:一個action中有兩個方法,add與sub, 它們都與execute有相同的參數個數和參數類型
public class Test1Action extends DispatchAction {
public ActionForward sub(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response)
throws Exception {
//SessionContainer a=this.getSessionContainer(request);
System.out.println("+++++++++sub++++++++++++");
return null;
}
public ActionForward add(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response)
throws Exception {
System.out.println("+++++++++++add+++++++++++");
return null;
}
}
3、在struts.xml中進行配置
<action path="/test1"
type="com.qf.struts.action.Test1Action"
validate="false"
parameter="method" />
4、http://localhost:8080/test/test1.do?method=sub
http://localhost:8080/test/test1.do?method=add
分別測試
action的parameter屬性決定了URL后跟的參數變量
三、找不到action的檢查方法
1、首先檢查action中的方法是否是那四個參數,以及是否 throws 了Exception
2、檢查action的配置文件是否配置了parameter 屬性。
總結
以上是生活随笔為你收集整理的内置的struts Action 类----DispatchAction的全部內容,希望文章能夠幫你解決所遇到的問題。
                            
                        - 上一篇: iBatis学习第一天
 - 下一篇: 单位框架学习总结