Struts MappingDispatchAction class is used to group similar functionality into a single action class, and execute the function depends on parameter attribute of the corresponding ActionMapping. Here’s an example to show the use of MappingDispatchAction.

1. MappingDispatchAction class

Extends the MappingDispatchAction class, and declares two methods – generateXML() and generateExcel().

MyCustomDispatchAction.java

package com.mkyong.common.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.MappingDispatchAction; public class MyCustomDispatchAction extends MappingDispatchAction{ public ActionForward generateXML(ActionMapping mapping,ActionForm form,
HttpServletRequest request,HttpServletResponse response)
throws Exception { request.setAttribute("method", "generateXML is called"); return mapping.findForward("success");
} public ActionForward generateExcel(ActionMapping mapping,ActionForm form,
HttpServletRequest request,HttpServletResponse response)
throws Exception { request.setAttribute("method", "generateExcel is called"); return mapping.findForward("success");
}
}

2. Struts configuration

Declares two action mappings, each point to same MyCustomDispatchAction class with different parameter attributes.

struts-config.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts-config PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 1.3//EN"
"http://jakarta.apache.org/struts/dtds/struts-config_1_3.dtd"> <struts-config> <action-mappings> <action
path="/CustomDispatchActionXML"
type="com.mkyong.common.action.MyCustomDispatchAction"
parameter="generateXML"
> <forward name="success" path="/pages/DispatchExample.jsp"/> </action> <action
path="/CustomDispatchActionExcel"
type="com.mkyong.common.action.MyCustomDispatchAction"
parameter="generateExcel"
> <forward name="success" path="/pages/DispatchExample.jsp"/> </action> <action
path="/Test"
type="org.apache.struts.actions.ForwardAction"
parameter="/pages/TestForm.jsp"
>
</action> </action-mappings> </struts-config>

3. View page

In JSP page, the links work as following :

  1. /CustomDispatchActionXML will execute the generateXML() method.
  2. /CustomDispatchActionExcel will execute the generateExcel() method.

TestForm.jsp

<%@taglib uri="http://struts.apache.org/tags-html" prefix="html"%>

Struts - DispatchAction Example

html:link

           Generate XML File

   |

           Generate Excel File

a href

           Generate XML File

   |

           Generate Excel File

DispatchExample.jsp

<%@taglib uri="http://struts.apache.org/tags-bean" prefix="bean"%>
<%@taglib uri="http://struts.apache.org/tags-logic" prefix="logic"%> Struts - DispatchAction Example

4. Test it

http://localhost:8080/StrutsExample/Test.do

If the “Generate XML File” link is clicked, it will forward to http://localhost:8080/StrutsExample/CustomDispatchActionXML.do

If the “Generate Excel File” link is clicked, it will forward to http://localhost:8080/StrutsExample/CustomDispatchActionExcel.do

Struts – MappingDispatchAction Example的更多相关文章

  1. Struts 笔记 内部资料 请勿转载 谢谢合作

    Struts 概述 随着MVC 模式的广泛使用,催生了MVC 框架的产生.在所有的MVC 框架中,出现最早,应用最广的就是Struts 框架. Struts 的起源 Struts 是Apache 软件 ...

  2. 菜鸟学Struts2——Struts工作原理

    在完成Struts2的HelloWorld后,对Struts2的工作原理进行学习.Struts2框架可以按照模块来划分为Servlet Filters,Struts核心模块,拦截器和用户实现部分,其中 ...

  3. Struts的拦截器

    Struts的拦截器 1.什么是拦截器 Struts的拦截器和Servlet过滤器类似,在执行Action的execute方法之前,Struts会首先执行Struts.xml中引用的拦截器,在执行完所 ...

  4. Struts框架的核心业务

    Struts的核心业务 Struts核心业务有很多,这里主要介绍了比较简单一些的: 请求数据的处理,和数据自动封装,类型自动转换 1.Struts中数据处理 1.1.方式1:直接过去servletap ...

  5. Struts的文件上传下载

    Struts的文件上传下载 1.文件上传 Struts2的文件上传也是使用fileUpload的组件,这个组默认是集合在框架里面的.且是使用拦截器:<interceptor name=" ...

  6. 配置hibernate,Struts。文件

    hibernate文件配置 <?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE hibernat ...

  7. hibernate与Struts框架结合编写简单针对修改练习

    失败页面fail.jsp <%@ page language="java" contentType="text/html; charset=UTF-8" ...

  8. 3. 解析 struts.xml 文件

    1. struts.xml 文件基本配置: 主要放在资源路径下,配置 sturts2相关的 Action , 拦截器等配置 <struts> <!-- 设置常量 --> < ...

  9. Struts+Spring+Hibernate项目的启动线程

    在Java Web项目中,经常要在项目开始运行时启动一个线程,每隔一定的时间就运行一定的代码,比如扫描数据库的变化等等.要实现这个功能,可以现在web.xml文件中定义一个Listener,然后在这个 ...

随机推荐

  1. 设计模式之单例模式实现(C++)

    #ifndef SINGLETON_H #define SINGLETON_H #include <cassert> #include <memory> #include &l ...

  2. 题解【bzoj2733 [HNOI2012]永无乡】

    Descriprition 两种操作 把两个集合并起来 求一个集合中的第 \(k\) 大(的编号) \(n \leq 10^5\) Solution 平衡树的板子题之一 维护两个点连不连通直接并查集 ...

  3. 完美解决github访问速度慢

    1. 解决方法 2.解决方法 1. 修改本地hosts文件 windows系统的hosts文件的位置如下:C:\Windows\System32\drivers\etc\hosts mac/linux ...

  4. bzoj千题计划157:bzoj1220:[HNOI2002]跳蚤

    扩展欧几里得:ax+by=gcd(a,b) 一定有解 能跳到左边一格,即ax+by=-1 若a,b的gcd=1,则一定有解 所以问题转化为 求n个不大于m的数,他们与m的gcd=1 的方案数 容斥原理 ...

  5. nodejs使用记录

    安装 下载64or32的安装程序,狂点下一步,无脑安装.然后检查一下: npm 使用npm -v命令检查npm是否可用 然后我们就可以使用npm了,npm语法如下: npm install <M ...

  6. angularJS $http $q $promise

    一天早晨,爹对儿子说:“宝儿,出去看看天气如何!” 每个星期天的早晨,爹都叫小宝拿着超级望远镜去家附近最高的山头上看看天气走势如何,小宝说没问题,我们可以认为小宝在离开家的时候给了他爹一个promis ...

  7. 另类之将ipython notebook嵌入blog方法

    另类之将ipython notebook嵌入blog方法 ipynb文件很强大. 可是一直苦于没有找到好的方法把它直接嵌入到博文里. 现在得到一个另类的方法: 就是利用github集成了nbviewe ...

  8. 自己写的一个小的剪刀——石头——布游戏的GUI程序

    很简单的一个程序,建议各位初学Java的同学可以试试写写这个程序: import javax.swing.JOptionPane; public class Game { public static ...

  9. linux 自定义yum仓库、repo文件 yum命令

    目录 自定义yum仓库:createrepo 自定义repo文件 使用yum命令安装httpd软件包 卸载httpd软件包:yum –y remove 软件名 清除yum缓存:yum clean al ...

  10. 截取汉字 mb_sbstr()

    一.中文截取:mb_substr() mb_substr( $str, $start, $length, $encoding ) $str,需要截断的字符串 $start,截断开始处,起始处为0 $l ...