Struts – MappingDispatchAction Example
2015-08-28 10:26
369 查看
Struts
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
Struts-MappingDispatchAction-2-xml-example
If the “Generate Excel File” link is clicked, it will forward to http://localhost:8080/StrutsExample/CustomDispatchActionExcel.do
Struts-MappingDispatchAction-3-excel-example
MappingDispatchActionclass 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 theMappingDispatchActionclass, 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 sameMyCustomDispatchActionclass 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 :/CustomDispatchActionXMLwill execute the
generateXML()method.
/CustomDispatchActionExcelwill 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
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
Struts-MappingDispatchAction-2-xml-example
If the “Generate Excel File” link is clicked, it will forward to http://localhost:8080/StrutsExample/CustomDispatchActionExcel.do
Struts-MappingDispatchAction-3-excel-example
相关文章推荐
- 转-android图片降低图片大小保持图片清晰的方法
- 浅析C#(WPF)中的Application类
- 浅析C#(WPF)中的Application类
- ios获取当前屏幕所显示Controller
- Idea、AndroidStudio快捷键整理
- 使用validate进行表单验证时土方法(appendTo)改变error显示的位置
- How to read Android crash log and stack trace
- Android各种访问权限Permission详解
- iOS 后台播放音乐
- Android SDK体系简析
- Q_OBJECT宏的作用
- Android中级篇之百度地图SDK v3.5.0-Hello BaiduMap[图解第一次运行百度地图]
- 微信统一支付接口返回“签名错误”的可能原因
- iOS开发多线程篇—多线程简单介绍
- iOS 7 tabbar 透明的问题
- 解决Android Studio更新SDK下载慢的问题
- Android微信智能心跳方案
- QT:线程、事件与QObject
- 基于AndroidPN的消息推送应用
- ios键盘类型设置