Struts – MappingDispatchAction Example
来源:互联网 发布:中国少儿编程 logo 编辑:程序博客网 时间:2024/05/19 19:55
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.javapackage 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 :
/CustomDispatchActionXML
will execute thegenerateXML()
method./CustomDispatchActionExcel
will execute thegenerateExcel()
method.
TestForm.jsp
<%@taglib uri="http://struts.apache.org/tags-html" prefix="html"%>Struts - DispatchAction Examplehtml:link Generate XML File | Generate Excel Filea 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
- Struts – MappingDispatchAction Example
- struts 中MappingDispatchAction 和 DispatchAction
- struts中的DispatchAction、LookupDispatchAction、MappingDispatchAction
- Struts – Wildcards example
- Struts – Multiple configuration files example
- Struts 2 – Development mode example
- Struts 2 – Resource bundle example
- Struts 2 – i18n or localization example
- Struts 2 – Development mode example
- 转摘---struts中的MappingDispatchAction类
- 在Struts中使用MappingDispatchAction 将一系列相关功能模块化
- Struts Hello World Example
- Struts ForwardAction Example
- Struts DispatchAction Example
- Struts DynaActionForm example
- Struts 2 ModelDriven example
- Example #1: Struts with Hibernate
- a example about struts tag
- 找出数组中重复的数
- Linux的tail 命令
- Spring MVC入门案例(1)
- nyoj1016德莱联盟【判断两线段是否相交】
- jQuery EasyUI和PHP实现数据的CURD操作
- Struts – MappingDispatchAction Example
- Windows Server 2008 如何部署Domain Controller
- Effective OC: 6. 7. 8
- PHP 单引号与双引号的区别
- IMX6Solo启动流程-从Uboot到kernel 中
- spring mvc
- hdu1754-I Hate It(线段树)
- 不使用临时变量交换两个数的值
- 买点已现!阅兵前线指挥车同款电视降价大促