1、DispatchAction
注:action扩展的是DispathAction 类
public
class
DispatchExampleAction
extends
DispathAction
...
{
2、MappingDispatchAction
注:action扩展的是MappingDispatchAction 类
public
class
MappingDispatchExampleAction
extends
MappingDispatchAction
...
{
struts-config.xml
<
action
path
="/mapping-foo"
type
="org.apache.struts.webapp.dispatch.MappingDispatchExampleAction"
parameter
="doFoo"
name
="testForm"
scope
="request"
>
<
forward
name
="success"
path
="/mapping.jsp"
/>
</
action
>
jsp中使用
<
html:form
action
="mapping-foo"
style
="display:inline"
>
<
html:submit
><
bean:message
key
="button.foo.label"
/></
html:submit
>
</
html:form
>
//注: 系统直接根据 parameter="doFoo" 去调用MappingDispatchExampleAction中的doFoo方法,因为客户端没有传回参数“doFoo”对应的值,如果客户端设置doFoo = execute则会调用execute方法。
3、LookupDispatchAction
注:action扩展的是LookupDispatchAction 类
public
class
LookupDispatchExampleAction
extends
LookupDispatchAction
...
{
struts-config.xml
<
action
path
="/lookup-submit"
type
="org.apache.struts.webapp.dispatch.LookupDispatchExampleAction"
parameter
="dispatchParam"
name
="testForm"
scope
="request"
>
<
exception
key
="dispatch.ServletException"
type
="javax.servlet.ServletException"
path
="/lookup.jsp"
/>
<
forward
name
="success"
path
="/lookup.jsp"
/>
</
action
>
jsp中使用
<
html:form
action
="lookup-submit"
style
="display:inline"
>
<
html:submit
property
="dispatchParam"
><
bean:message
key
="button.foo.label"
/></
html:submit
>
<
html:submit
property
="dispatchParam"
><
bean:message
key
="button.bar.label"
/></
html:submit
>
<
html:submit
property
="dispatchParam"
><
bean:message
key
="button.invalid.label"
/></
html:submit
>
<
html:submit
property
="wrongParam"
><
bean:message
key
="parameter.wrong.label"
/></
html:submit
>
</
html:form
>
注意action中的写法:
LookupDispatchExampleAction中设置
public
class
LookupDispatchExampleAction
extends
LookupDispatchAction
...
{ private Map keyMethodMap = new HashMap(); /** */ /** * Constructor - populate the key method map. */ public LookupDispatchExampleAction() ... { keyMethodMap.put( " button.foo.label " , " doFoo " ); keyMethodMap.put( " button.bar.label " , " doBar " ); } ...... /** */ /** * Provides the mapping from resource key to method name. * * @return Resource key / method name map. */ protected Map getKeyMethodMap() ... { return keyMethodMap; } }
//
action覆盖方法getKeyMethodMap(),系统自动根据传回来的参数去map中找是否存在对应的key,method映射,这个key是在资源文件中对应的key.
4、EventDispatchAction
注: EventDispatchActionExample 扩展的是 EventDispatchAction
public
class
EventDispatchActionExample
extends
EventDispatchAction
...
{
struts-config.xml
<
action
path
="/eventAction-default"
type
="org.apache.struts.webapp.dispatch.EventDispatchActionExample"
parameter
="doFoo,bar=doBar,default=doBar"
name
="testForm"
scope
="request"
>
<
forward
name
="success"
path
="/eventAction.jsp"
/>
</
action
>
jsp中的使用
<
html:form
action
="eventAction-default"
style
="display:inline"
>
<
html:submit
property
="doFoo"
><
bean:message
key
="button.foo.label"
/></
html:submit
>
</
html:form
>
注: parameter ="doFoo,bar=doBar,default=doBar" 其中parameter是关于事件触发的描述, < html:submit property ="doFoo" >触发doFoo方法, < html:submit property ="doBar" >或< html:submit property ="bar" >都能触发doBar方法,bar是doBar的别名,如果不设定该参数,那么默认调用的就是doBar,因为: default=doBar
------------------------------------------------------------------------------------------------------------------------------------- 以上都是扩展的Action的子类,以下直接扩展 Action
5、ActionDispatcher
注:action扩展的是Action 类
public
class
ActionDispatcherExample
extends
Action
...
{
struts-config.xml
<
action
path
="/actionDispatcher-submit"
type
="org.apache.struts.webapp.dispatch.ActionDispatcherExample"
parameter
="actionDispatcherMethod"
name
="testForm"
scope
="request"
>
<
exception
key
="dispatch.NoSuchMethodException"
type
="java.lang.NoSuchMethodException"
path
="/actionDispatcher.jsp"
/>
<
exception
key
="dispatch.ServletException"
type
="javax.servlet.ServletException"
path
="/actionDispatcher.jsp"
/>
<
forward
name
="success"
path
="/actionDispatcher.jsp"
/>
</
action
>
jsp中的使用
<
html:form
action
="actionDispatcher-submit"
style
="display:inline"
>
<
input
type
="hidden"
name
="actionDispatcherMethod"
value
="doFoo"
/>
<
html:submit
><
bean:message
key
="button.foo.label"
/></
html:submit
>
</
html:form
>
//没有找出与1、的区别
6、EventActionDispatcher
注:action扩展的还是基本的 Action,但是execute方法的实现有所改变
public
class
EventActionDispatcherExample
extends
Action
...
{ private ActionDispatcher dispatcher = new EventActionDispatcher( this ); private int fooCount; private int barCount; /** */ /** * Execute method. * * @param mapping The ActionMapping used to select this instance * @param form The optional ActionForm bean for this request * @param request The servlet request we are processing * @param response The servlet response we are creating * * @exception Exception if business logic throws an exception */ public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception ... { return dispatcher.execute(mapping, form, request, response); } ...... }
struts-config.xml和jsp中的使用方法同5,因为如果没有对应的方法可调用的话,会调用EventActionDispatcher的execute方法