An abstract Action that dispatches to a public method that is named by the request parameter whose name is specified by the parameter
property of the corresponding ActionMapping. This Action is useful for developers who prefer to combine many similar actions into a single Action class, in order to simplify their application design.
To configure the use of this action in your struts-config.xml
file, create an entry like this:
<action path="/saveSubscription" type="org.apache.struts.actions.DispatchAction" name="subscriptionForm" scope="request" input="/subscription.jsp" parameter="method"/>
which will use the value of the request parameter named "method" to pick the appropriate "execute" method, which must have the same signature (other than method name) of the standard Action.execute method. For example, you might have the following three methods in the same action:
- public ActionForward delete(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception
- public ActionForward insert(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception
- public ActionForward update(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception
and call one of the methods with a URL like this:
http://localhost:8080/myapp/saveSubscription.do?method=update
NOTE - All of the other mapping characteristics of this action must be shared by the various handlers. This places some constraints over what types of handlers may reasonably be packaged into the same DispatchAction
subclass.
NOTE - If the value of the request parameter is empty, a method named unspecified
is called. The default action is to throw an exception. If the request was cancelled (a html:cancel
button was pressed), the custom handler cancelled
will be used instead. You can also override the getMethodName
method to override the action's default handler selection.
从第一段可以知道,这就是我们所要找的那个它了,
<action path="/saveSubscription" type="org.apache.struts.actions.DispatchAction" name="subscriptionForm" scope="request" input="/subscription.jsp" parameter="method"/>
还有这里有一个配置的例子 ,它的意思就是说,这个Action提供了一个方法让我们跟椐parameter="method" (这里是method)让我们调用Action中的不同的方法,当然这些方法跟我们的execute是一样的只是名字不一样。例如下面:
public ActionForward execute(ActionMapping arg0, ActionForm arg1,
HttpServletRequest arg2, HttpServletResponse arg3) throws Exception {
return super.execute(arg0, arg1, arg2, arg3);
}
//跟上面对比一下,是不是一样呢
public ActionForward sayHello(ActionMapping arg0, ActionForm form,
HttpServletRequest request, HttpServletResponse response) throws Exception {
response.getWriter().write("Hello ! DidpatchAction");
response.flushBuffer();
return null;
}
public ActionForward closeWindow(ActionMapping arg0, ActionForm form,
HttpServletRequest request, HttpServletResponse response) throws Exception {
String str ="IE Will Close<script language=\"javascript\">window.close();</script>";
response.getWriter().write(str);
response.flushBuffer();
return null;
好下面我们在以前的例子的基础上完成这次的例子
新建一个struts-config-action.xml文件,下面为文件的内容:
<?xml version="1.0" encoding="UTF-8" ?>
<!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="/testAction" validate="false" parameter="method"
type="org.action.struts.DidpatchActionTest">
</action>
</action-mappings>
</struts-config>
是不是看不习惯呢,没有Action的也没有forward。不过现在我告诉你这是完全可以的
我们把这个配置文件加到web.xml文件中
<init-param>
<param-name>config</param-name>
<param-value>/WEB-INF/struts-config.xml,/WEB-INF/struts-config-file.xml,/WEB-INF/struts-config-action.xml</param-value>
</init-param>
以这种目录结构建一个index.jsp文件
下面为jsp的内容:
<?xml version="1.0" encoding="UTF-8" ?>
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ taglib uri="http://struts.apache.org/tags-bean" prefix="bean" %>
<%@ taglib uri="http://struts.apache.org/tags-html" prefix="html" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>Action Test</title>
</head>
<body>
<a href="../testAction.do?method=sayHello">sayHello</a>
<a href="../testAction.do?method=closeWindow">closeWindow</a>
</body>
</html>
没有什么好说的。
下面我们建一个Action,当然基类是org.apache.struts.actions.DispatchAction
下面为Action的内容:
package org.action.struts;
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.DispatchAction;
public class DidpatchActionTest extends DispatchAction {
public ActionForward execute(ActionMapping arg0, ActionForm arg1,
HttpServletRequest arg2, HttpServletResponse arg3) throws Exception {
return super.execute(arg0, arg1, arg2, arg3);
}
//跟上面对比一下,是不是一样呢
public ActionForward sayHello(ActionMapping arg0, ActionForm form,
HttpServletRequest request, HttpServletResponse response) throws Exception {
response.getWriter().write("<script language=\"javascript\">alert('Hello ! DidpatchAction');</script>");
response.flushBuffer();
return null;
}
public ActionForward closeWindow(ActionMapping arg0, ActionForm form,
HttpServletRequest request, HttpServletResponse response) throws Exception {
String str ="IE Will Close<script language=\"javascript\">window.close();</script>";
response.getWriter().write(str);
response.flushBuffer();
return null;
}
}
我们用response对象来输出页面的内容(这个与jsp的response对象是一样的);
下面为运行的结果
大家看到了没有,http://localhost:8080/Struts_HelloWorld/testAction.do?method=closeWindow
它会跟椐method中的参数来调用不同的函数