在struts1中使用DispatchAction对应多个ActionForm,实现写一个Action实现增删改查操作---一个Action处理多个操作的能力

来源:互联网 发布:vr视频剪辑制作软件 编辑:程序博客网 时间:2024/05/22 06:51

先来了解DispatchAction类:

原始的配置action都是使用一个Action类实现一个功能,当工程项目很大的时候,Action类数目会很多,导致代码文件膨胀,这就增加了该项目后期的维护难度。

DispatchAction,就是用来解决使用一个Action处理多个操作的能力,这是DispatchAction最大的用途,它可以帮助我们用一个Action类,封装一套类似的操作方法,节省书写Action类的数目,同时也减轻了后期维护的困难。

DispatchAction类是一个抽象类,它实现了父类(Action)的execute()方法,所以它的子类就不用来实现这个方法了,只需要专注与实际操作的方法。


下面是一个具体的实例:

(1)在使用时,要一个DispatchAction的子类,它含有一些方法,login,logout,method1

                                                                                
package examples; 
                                                                                
import javax.servlet.http.*; 
import org.apache.struts.action.*; 
import org.apache.struts.actions.*; 
public class AccountAction extends DispatchAction { 
    public ActionForward login(ActionMapping mapping, 
                              ActionForm form, 
                              HttpServletRequest request, 
                              HttpServletResponse response) 
    throws Exception { 
        //进行一些Login的逻辑
              return mapping.findForward("success");   


    } 
                                                                                
    public ActionForward logout(ActionMapping mapping, 
                                ActionForm form, 
                                HttpServletRequest request, 
                                HttpServletResponse response) 
    throws Exception { 
        // 进行一些Logout的逻辑
              return mapping.findForward("success1");   


    } 


    public ActionForward method1(ActionMapping mapping, 
                                ActionForm form, 
                                HttpServletRequest request, 
                                HttpServletResponse response) 
    throws Exception { 
        // 进行一些method1的逻辑
              return mapping.findForward("success");   


    } 
 




     
一定要注意在DispatchAction中你想执行的操作,都必须要有统一的参数(ActionMapping mapping, ActionForm form,HttpServletRequest request, HttpServletResponse response),是一个规定


(2)接下来是配置struts-config.xml 
  
    <action path="/login"  type="examples.AccountAction"  
             name="logonForm" 
             scope="request" 
             parameter="action" 
             input="/pages/dispatch1.jsp"> 
       <forward name="success" path="/pages/success.jsp" /> 
    </action> 


    <action path="/logout"  type="examples.AccountAction"  
             name="logonForm" 
             scope="request" 
             parameter="action" 
             input="/pages/dispatch1.jsp"> 
       <forward name="success1" path="/pages/success1.jsp" /> 
    </action> 


   
这里需要注意的就是parameter属性的值,因为这个值要和页面传来的参数对应. 


(3)再来看看JSP页 pages/dispatch1.jsp 
  
<%@ taglib uri="/tags/struts-html" prefix="html" %> 
<html:link href="/DispathActionTest/login.do?action=login">login</html:link><br> 
<html:link href="/DispathActionTest/logout.do?action=logout">logout</html:link> 




    这里
要注意几点,首先,?后面的KEY要和struts-config.xml中的parameter相同,还有它的VALUE要是你在action的一个方法名字,这里方法名为login, 那么在程序运行时就是调用login的操作,如果是logout,那程序就调用logout的操作.

1 0