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. mysql 统计 group by 之后的 group 的个数

    如果将 count(*) 和 group by 一起使用,count(*) 统计的将会是每个 group 里面的行数,而不是 group 的个数. 如果你想统计 group 的个数,需要将 group ...

  2. springcloud的分布式配置Config

    1.为什么要统一配置管理? 微服务由多个服务构成,多个服务多个配置,则对这些配置需要集中管理.不同环境不同配置,运行期间动态调整,自动刷新. 统一管理微服务的配置:分布式配置管理的一些组件: zook ...

  3. hackerrank答案

    regex: https://github.com/taiyang-li/hackerrank/tree/master/hackerrank/regex-exercise awk: https://g ...

  4. 获取异常信息e.printStackTrace()的内容

    获取异常信息e.printStackTrace()的内容 最近做项目的时候需要记录操作的日志,但是记录异常信息的是发现使用e.getMessage()根本无法满足需要,并且e.getMessage() ...

  5. numpy计算路线距离

    numpy计算路线距离 觉得有用的话,欢迎一起讨论相互学习~Follow Me 参考文献 enumerate遍历数组 np.diff函数 numpy适用数组作为索引 标记路线上的点 \[X={X1,X ...

  6. 离线下载pip包安装

    Host-A 不能上网,但是需要在上面安装python-package 通过另外一台能上网的Host-B主机 1. 下载需要离线安装的Packages 在Host-B上执行如下命令: 安装单个Pack ...

  7. CF&&CC百套计划2 CodeChef December Challenge 2017 Chef and Hamming Distance of arrays

    https://www.codechef.com/DEC17/problems/CHEFHAM #include<cstdio> #include<cstring> #incl ...

  8. 20145226夏艺华 《Java程序设计》第5周学习总结

    教材学习内容总结 第八章 异常处理 语法与继承架构 使用 try.catch Java中所有信息都会被打包为对象,如果愿意,可以尝试(try)捕捉(catch)代表错误的对象后做一些处理 try{ . ...

  9. 20155322 2016-2017-2 《Java程序设计》第5周学习总结

    20155322 2016-2017-2 <Java程序设计>第5周学习总结 教材学习内容总结 本周的学习任务是课本第八第九章: 第八章主要是讲异常处理.这里要理解Java的错误以对象的方 ...

  10. 《C语言程序设计基础I》秋季学习总结

    希望下学期比这学期轻松,学习能力上升,只是越发丰富. 一步一步的走踏实了