struts1_DispatchAction 可以同时处理多个请求的Action

下面是一段:html请求

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@taglib uri="http://struts.apache.org/tags-html" prefix="html" %>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <title>My JSP '1.jsp' starting page</title>
  </head>
  
  <body>
    
    <html:link action="/BookAction?method=add">添加图书</html:link>
     <html:link action="/BookAction?method=delete">删除图书</html:link>
      <html:link action="/BookAction?method=update">修改图书</html:link>
       <html:link action="/BookAction?method=find">查找图书</html:link>
    
   <br/> <br/>---------------------------------------------<br/><br/>
    
    <html:link action="/addbook">添加图书</html:link>
     <html:link action="/deletebook">删除图书</html:link>
      <html:link action="/updatebook">修改图书</html:link>
       <html:link action="/findbook">查找图书</html:link>
    
  </body>
</html>

下面是配置文件:struts-config.xml

<?xml version="1.0" encoding="UTF-8" ?>

<!DOCTYPE struts-config PUBLIC
          "-//Apache Software Foundation//DTD Struts Configuration 1.3//EN"
          "http://struts.apache.org/dtds/struts-config_1_3.dtd">

<struts-config>
    
    
    <action-mappings>
        <action path="/BookAction" 
                type="cn.itcast.web.action.BookAction"
                parameter="method"> <!-- 告诉struts,要调用的方法名称是通过什么参数带过来的 -->
        </action>
        
        
    </action-mappings>

</struts-config>

下面是处理请求的java代码:

//DispatchAction---action  ---execute()  add 

/*
   String method = mapping.getParamter();//拿到配置的参数的名称:method
   String methodName = request.getParameter(method);  //add update拿到的是请求参数method对应的值:add还是delete。。。
   然后反射BookAction,调用对应的请求方法
   
 * 
 */
public class BookAction extends DispatchAction {

    
    public ActionForward add(ActionMapping arg0, ActionForm arg1,
            HttpServletRequest arg2, HttpServletResponse arg3) throws Exception {
        
        System.out.println("add....");
        return null;
    }
    
    public ActionForward update(ActionMapping arg0, ActionForm arg1,
            HttpServletRequest arg2, HttpServletResponse arg3) throws Exception {
        // TODO Auto-generated method stub
        
        System.out.println("udpate....");
        return null;
    }
    
    public ActionForward delete(ActionMapping arg0, ActionForm arg1,
            HttpServletRequest arg2, HttpServletResponse arg3) throws Exception {
        // TODO Auto-generated method stub
        
        System.out.println("delete....");
        return null;
    }
    
    public ActionForward find(ActionMapping arg0, ActionForm arg1,
            HttpServletRequest arg2, HttpServletResponse arg3) throws Exception {
        // TODO Auto-generated method stub
        System.out.println("find....");
        return null;
    }
    
}


转载于:https://my.oschina.net/cys21/blog/491572

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值