DispatchAction class

Class Dispatch is used to create actions that can handle more than one action which is execute(). Instead of using only execute() method, we can add many methods like add(), edit(), delete() in a action class.

1. create class which extends org.apache.struts.actions.DispatchAction;
2. config the action in struts-config.xml. We have to add a attribute parameter="method", and we have to refer to the method like /studentDispatchAction?method=add.
3. using dispachtAction in .jsp files.

A example:
1.

------------------------------------------------------------
StudentDispatchAction.java
-----------------------------

package  studentInfo;

import  java.io.IOException;
import  java.util.ArrayList;

import  javax.servlet.ServletException;
import  javax.servlet.http.HttpServletRequest;
import  javax.servlet.http.HttpServletResponse;

import  org.apache.struts.action.ActionForm;
import  org.apache.struts.action.ActionForward;
import  org.apache.struts.action.ActionMapping;
import  org.apache.struts.actions.DispatchAction;

public   class  StudentDispatchAction  extends  DispatchAction  {
 
public ActionForward add(ActionMapping mapping, ActionForm form,
   HttpServletRequest request, HttpServletResponse response)
   
throws IOException, ServletException {

  StudentForm stuForm 
= (StudentForm) form;
  String stuID 
= stuForm.getStuID();
  String name 
= stuForm.getName();
  String gender 
= stuForm.getGender();
  String age 
= stuForm.getAge();
  ArrayList al 
= new ArrayList();
  al.add(stuID);
  al.add(name);
  al.add(gender);
  al.add(age);

  String prompt;
  StudentDAO stuDAO 
= new StudentDAO();
  
if (stuDAO.savaData(al)) {
   prompt 
= "Success";
  }
 else {
   prompt 
= "Fail";
  }

  
return mapping.findForward(prompt);
 }

 
  
public ActionForward edit(ActionMapping mapping, ActionForm form,
    HttpServletRequest request, HttpServletResponse response)
    
throws IOException, ServletException {

   
//...
   
   String prompt 
= "Fail";
   
return mapping.findForward(prompt);
  }

}

------------------------------------------------------------

2. config struts-config.xml


    
< action  input ="/studentInfo/register.jsp"  name ="studentForm"
     path
="/studentDispatchAction"  scope ="request"
     type
="studentInfo.StudentDispatchAction"  validate ="true"
     parameter
="method" >
     
< forward  name ="Success"  path ="/studentInfo/success.jsp"
      redirect
="true"   />
     
< forward  name ="Fail"  path ="/studentInfo/fail.jsp"  redirect ="true"   />
    
</ action >

--------

3. using it in .jsp files

< html:form  method ="post"  action ="/studentDispatchAction?method=add" >

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值