zoukankan      html  css  js  c++  java
  • Struts – MappingDispatchAction Example

    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
    Struts-MappingDispatchAction-1-example

    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

  • 相关阅读:
    大数据和云计算如何实现视频行业更快速有效的智能分析?
    TSINGSEE青犀视频云边端架构流媒体平台的接口鉴权和接口保活是什么关系?
    TSINGSEE青犀视频和海康合作研发RTMP摄像头如何通过内存卡进行视频录像存储?
    RTMP视频推流功能组件EasyRTMP-HIK DEMO版本运行报错0xc000007b问题排查分析
    互联网视频直播&点播平台RTMP推流组件EasyRTMP如何获取当前推流状态 ?
    程序员修炼之道阅读笔记(一)
    周进度总结
    2020年秋季个人阅读计划
    周进度总结
    Java的Swing布局
  • 原文地址:https://www.cnblogs.com/ghgyj/p/4766312.html
Copyright © 2011-2022 走看看