`

struts 1.x 和通配符的使用 MappingDispatchAction

阅读更多

环境多文件配置<?xml version="1.0" encoding="UTF-8"?>

<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">
  <display-name>TomcatEjb</display-name>
  
  <!-- Standard Action Servlet Configuration -->
  <servlet>
    <servlet-name>action</servlet-name>
    <servlet-class>org.apache.struts.action.ActionServlet</servlet-class>
    <init-param>
      <param-name>config</param-name>
      <param-value>/WEB-INF/struts-config.xml,/WEB-INF/struts-test.xml</param-value>
    </init-param>
    <load-on-startup>2</load-on-startup>
 </servlet>

  <!-- Standard Action Servlet Mapping -->
  <servlet-mapping>
    <servlet-name>action</servlet-name>
    <url-pattern>*.do</url-pattern>
  </servlet-mapping>
  
  <welcome-file-list>
    <welcome-file>index.html</welcome-file>
    <welcome-file>index.htm</welcome-file>
    <welcome-file>index.jsp</welcome-file>
    <welcome-file>default.html</welcome-file>
    <welcome-file>default.htm</welcome-file>
    <welcome-file>default.jsp</welcome-file>
  </welcome-file-list>
</web-app>

 MappingDispatchAction配置文件

 

org.apache.struts.actions 
Class MappingDispatchAction

java.lang.Object
  

extended by

org.apache.struts.action.Action
      

extended by

org.apache.struts.actions.BaseAction
          

extended by

org.apache.struts.actions.DispatchAction
              

extended by

org.apache.struts.actions.MappingDispatchAction

public class MappingDispatchAction
extends DispatchAction

 

An abstract Action that dispatches to a public method that is named by the parameter attribute of the corresponding ActionMapping. This is useful for developers who prefer to combine many related actions into a single Action class.

To configure the use of this action in your struts-config.xml file, create an entry like this:


   <action path="/saveSubscription"
           type="org.example.SubscriptionAction"
           name="subscriptionForm"
          scope="request"
          input="/subscription.jsp"
      parameter="method"/>
 

where 'method' is the name of a method in your subclass of MappingDispatchAction that has the same signature (other than method name) of the standard Action.execute method. For example, you might combine the methods for managing a subscription into a single MappingDispatchAction class using the following methods:

 

例子通配符的使用

 

<?xml version="1.0" encoding="ISO-8859-1" ?>

<!DOCTYPE struts-config PUBLIC
          "-//Apache Software Foundation//DTD Struts Configuration 1.3//EN"
          "http://struts.apache.org/dtds/struts-config_1_3.dtd">

<struts-config>
  <action-mappings>
      <action path="/test/TestAction_*" type="com.test.action.TestAction" parameter="{1}TestAction">
          <forward name="add" path="/test/addTest.jsp" />
      </action>
  </action-mappings>

</struts-config>

 Action 对像

 

package com.test.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 TestAction extends MappingDispatchAction {
	

	//additionTest
	public ActionForward addTestAction(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response){
		request.setAttribute("usb", "Struts 1.x Addition Successful...");
		return mapping.findForward("add");
		
	}
	
	//additionTest
	public ActionForward deleteTestAction(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response){
		request.setAttribute("usb", "Struts 1.x Delete Successful...");
		return mapping.findForward("add");
		
	}
	
	//additionTest
	public ActionForward listTestAction(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response){
		request.setAttribute("usb", "Struts 1.x List Successful...");
		return mapping.findForward("add");
		
	}
	
	
	

}

 

 

  • 大小: 177.9 KB
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics