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. jenkins权限管理,不同用户显示不同项目

    1.安装Role-based Authorization Strategy插件 系统管理-管理插件-可选插件中安装Role-based Authorization Strategy 安装后重启jenk ...

  2. Service Fabric —— Actor / Stateless Service 概念

    作者:潘罡 (Van Pan) @ Microsoft 上一节我们谈到了Stateful Service.在Service Fabric中,Stateful Service是理解Micro Servi ...

  3. python 资产扫描01

    本地建立的三个文件: Asset1.txt 用来保存扫描到的资产 Asset2.txt 用来导入给定的资产 Repeat.txt 保存重复的资产 程序的功能: 1.资产扫描,以 位置:资产 格式保存到 ...

  4. Go_18: Golang 中三种读取文件发放性能对比

    Golang 中读取文件大概有三种方法,分别为: 1. 通过原生态 io 包中的 read 方法进行读取 2. 通过 io/ioutil 包提供的 read 方法进行读取 3. 通过 bufio 包提 ...

  5. 逻辑回归原理_挑战者飞船事故和乳腺癌案例_Python和R_信用评分卡(AAA推荐)

    sklearn实战-乳腺癌细胞数据挖掘(博客主亲自录制视频教程) https://study.163.com/course/introduction.htm?courseId=1005269003&a ...

  6. Typora 自定义主题 修改左右间距

    打开偏好设置,打开主题文件夹 比如要修改night主题中的间距,编辑night.css文件,修改#write样式即可. 修改其他样式类试.

  7. PHP基础知识之————匿名函数(Anonymous functions)

    匿名函数(Anonymous functions),也叫闭包函数(closures),允许 临时创建一个没有指定名称的函数.最经常用作回调函数(callback)参数的值.当然,也有其它应用的情况. ...

  8. Druid.io SQL乱码问题

    1.场景 1.1.依赖版本 avatica-core 1.11.0 druid 0.12.0 1.2.问题重现: 使用Avatica JDBC查询语句:SELECT score FROM studen ...

  9. 用代码从文件中导入数据到SQL Server

    引言 导入数据到SQL Server 是常见的需求,特别是定期导入这种需求. 对于定期导入主要有以下几种方式可选择: Bulk Insert Bcp Utility OpenRowSet 写程序导入( ...

  10. UNIX环境高级编程 第8章 进程控制

    本章是UNIX系统中进程控制原语,包括进程创建.执行新程序.进程终止,另外还会对进程的属性加以说明,包括进程ID.实际/有效用户ID. 进程标识 每个进程某一时刻在系统中都是独一无二的,它们之间是用一 ...