Dispatch-Struts-Examples1.3.8
l Dispatch Action
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.
以上是JavaDoc提供的描述,除了http://localhost:8080/myapp/saveSubscription.do?method=update
这种请求外,也可以使用定义一个名叫method的隐藏域。
Example就是定义了一个dispatchMethod的隐藏域.
如果jsp里的隐藏域定义的方法在action里没有定义会NoSuchMethodException
如果jsp里的隐藏域定义的方法是execute,会有异常ServletException
如果jsp里请求的action对应的mappingaction配置没有指定parameter,会有ServletException
l MappingDispachAction
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:
public ActionForward create(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception
public ActionForward edit(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception
public ActionForward save(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception
public ActionForward delete(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception
public ActionForward list(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception
for which you would create corresponding <action> configurations that reference this class:
<action path="/createSubscription"
type="org.example.SubscriptionAction"
parameter="create">
<forward name="success" path="/editSubscription.jsp"/>
</action>
<action path="/editSubscription"
type="org.example.SubscriptionAction"
parameter="edit">
<forward name="success" path="/editSubscription.jsp"/>
</action>
<action path="/saveSubscription"
type="org.example.SubscriptionAction"
parameter="save"
name="subscriptionForm"
validate="true"
input="/editSubscription.jsp"
scope="request">
<forward name="success" path="/savedSubscription.jsp"/>
</action>
<action path="/deleteSubscription"
type="org.example.SubscriptionAction"
name="subscriptionForm"
scope="request"
input="/subscription.jsp"
parameter="delete">
<forward name="success" path="/deletedSubscription.jsp"/>
</action>
<action path="/listSubscriptions"
type="org.example.SubscriptionAction"
parameter="list">
<forward name="success" path="/subscriptionList.jsp"/>
</action>
NOTE - Unlike DispatchAction, mapping characteristics may differ between the various handlers, so you can combine actions in the same class that, for example, differ in their use of forms or validation. Also, a request parameter, which would be visible to the application user, is not required to enable selection of the handler method
如果mappingaction配置的方法在action里没有定义会NoSuchMethodException
如果mappingaction配置的方法是execute,会有异常ServletException
如果jsp里请求的action对应的mappingaction配置没有指定parameter,会有ServletException
l LookupDispachAction
An abstract Action that dispatches to the subclass mapped execute
method. This is useful in cases where an HTML form has multiple submit buttons with the same name. The button name is specified by the parameter
property of the corresponding ActionMapping. To configure the use of this action in your struts-config.xml
file, create an entry like this:
<action path="/test"
type="org.example.MyAction"
name="MyForm"
scope="request"
input="/test.jsp"
parameter="method"/>
which will use the value of the request parameter named "method" to locate the corresponding key in ApplicationResources. For example, you might have the following ApplicationResources.properties:
button.add=Add Record
button.delete=Delete Record
And your JSP would have the following format for submit buttons:
<html:form action="/test">
<html:submit property="method">
<bean:message key="button.add"/>
</html:submit>
<html:submit property="method">
<bean:message key="button.delete"/>
</html:submit>
</html:form>
Your subclass must implement both getKeyMethodMap and the methods defined in the map. An example of such implementations are:
protected Map getKeyMethodMap() {
Map map = new HashMap();
map.put("button.add", "add");
map.put("button.delete", "delete");
return map;
}
public ActionForward add(ActionMapping mapping,
ActionForm form,
HttpServletRequest request,
HttpServletResponse response)
throws IOException, ServletException {
// do add
return mapping.findForward("success");
}
public ActionForward delete(ActionMapping mapping,
ActionForm form,
HttpServletRequest request,
HttpServletResponse response)
throws IOException, ServletException {
// do delete
return mapping.findForward("success");
}
Notes - If duplicate values exist for the keys returned by getKeys, only the first one found will be returned. If no corresponding key is found then an exception will be thrown. You can override the method unspecified
to provide a custom handler. If the submit was cancelled (a html:cancel
button was pressed), the custom handler cancelled
will be used instead.
l ActionDispatcher
Action helper class that dispatches to a public method in an Action.
This class is provided as an alternative mechanism to using DispatchAction and its various flavours and means Dispatch behaviour can be easily implemented into any Action without having to inherit from a particular super Action.
To implement dispatch behaviour in an Action class, create your custom Action as follows, along with the methods you require (and optionally "cancelled" and "unspecified" methods):
public class MyCustomAction extends Action {
protected ActionDispatcher dispatcher
= new ActionDispatcher(this, ActionDispatcher.MAPPING_FLAVOR);
public ActionForward execute(ActionMapping mapping,
ActionForm form,
HttpServletRequest request,
HttpServletResponse response)
throws Exception {
return dispatcher.execute(mapping, form, request, response);
}
}
It provides three flavours of determing the name of the method:
DEFAULT_FLAVOR - uses the parameter specified in the struts-config.xml to get the method name from the Request (equivalent to DispatchAction except uses "method" as a default if the parameter is not specified in the struts-config.xml).
DISPATCH_FLAVOR - uses the parameter specified in the struts-config.xml to get the method name from the Request (equivalent to DispatchAction).
MAPPING_FLAVOR - uses the parameter specified in the struts-config.xml as the method name (equivalent to MappingDispatchAction).
l EventDispatchAction
Action that dispatches to to one of the public methods that are named in the parameter
attribute of the corresponding ActionMapping and matches a submission parameter. This is useful for developers who prefer to use many submit buttons, images, or submit links on a single form and whose related actions exist in a single Action class.
The method(s) must have the same signature (other than method name) of the standard Action.execute method.
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="save,back,recalc=recalculate,default=save"/>
save
,back,recalc和submit的Property关联
where parameter
contains three possible methods and one default method if nothing matches (such as the user pressing the enter key).
For utility purposes, you can use the key=value
notation to alias methods so that they are exposed as different form element names, in the event of a naming conflict or otherwise. In this example, the recalc button (via a request parameter) will invoke the recalculate
method. The security-minded person may find this feature valuable to obfuscate and not expose the methods.
The default key is purely optional. If this is not specified and no parameters match the list of method keys, null
is returned which means the unspecified
method will be invoked.
The order of the parameters are guaranteed to be iterated in the order specified. If multiple buttons were accidently submitted, the first match in the list will be dispatched.
l EventActionDispatcher
An Action helper class that dispatches to to one of the public methods that are named in the parameter
attribute of the corresponding ActionMapping and matches a submission parameter. This is useful for developers who prefer to use many submit buttons, images, or submit links on a single form and whose related actions exist in a single Action class.
The method(s) in the associated Action
must have the same signature (other than method name) of the standard Action.execute method.
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="save,back,recalc=recalculate,default=save"/>
where parameter
contains three possible methods and one default method if nothing matches (such as the user pressing the enter key).
For utility purposes, you can use the key=value
notation to alias methods so that they are exposed as different form element names, in the event of a naming conflict or otherwise. In this example, the recalc button (via a request parameter) will invoke the recalculate
method. The security-minded person may find this feature valuable to obfuscate and not expose the methods.
The default key is purely optional. If this is not specified and no parameters match the list of method keys, null
is returned which means the unspecified
method will be invoked.
The order of the parameters are guaranteed to be iterated in the order specified. If multiple buttons were accidently submitted, the first match in the list will be dispatched.
To implement this dispatch behaviour in an Action
, class create your custom Action as follows, along with the methods you require (and optionally "cancelled" and "unspecified" methods):
public class MyCustomAction extends Action {
protected ActionDispatcher dispatcher = new EventActionDispatcher(this);
public ActionForward execute(ActionMapping mapping,
ActionForm form,
HttpServletRequest request,
HttpServletResponse response)
throws Exception {
return dispatcher.execute(mapping, form, request, response);
}
}
与ActionDispatcher类似,是一个helper类,为了使普通的action达到与EventDispatchAction一样的功能