struts中dispathAction

當在stuts中需要在一個action中定義多個方法時,可以使用dispathAction來完成

下面是測試的一個簡單的例子:

Action:

//Created by MyEclipse Struts
// XSL source (default): platform:/plugin/com.genuitec.eclipse.cross.easystruts.eclipse_4.1.0/xslt/JavaClass.xsl

package com.test.struts.action;

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;

/**
 * MyEclipse Struts
 * Creation date: 04-21-2006
 *
 * XDoclet definition:
 * @struts.action parameter="method"
 */
public class MyDispathAction extends DispatchAction {

 // --------------------------------------------------------- Instance Variables

 // --------------------------------------------------------- Methods

 /**
  * Method execute
  * @param mapping
  * @param form
  * @param request
  * @param response
  * @return ActionForward
  */

 public ActionForward Add(
   ActionMapping mapping,
   ActionForm form,
   HttpServletRequest request,
   HttpServletResponse response) {
  request.setAttribute("method", new String("add"));
  return mapping.findForward("sucess");
 }
 public ActionForward Delete(
   ActionMapping mapping,
   ActionForm form,
   HttpServletRequest request,
   HttpServletResponse response) {
  request.getSession().setAttribute("method", new String("delete"));
  return mapping.findForward("sucess");
 }
}

stuts-config.xml:

    <action
      parameter="method"
      path="/dispath"
      type="com.test.struts.action.MyDispathAction"
      validate="false" >
      <forward name="sucess" path="/sucess.jsp" />
     </action>

需要注意的是要將action中的execute方法刪除!!!

 

調用:

http://127.0.0.1:8080/test/dispath.do?method=Add

http://127.0.0.1:8080/test/dispath.do?method=Delete

分別調用Action中的兩種方法!!

發佈了38 篇原創文章 · 獲贊 1 · 訪問量 9萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章