struts 中的dispatch学习

1、DispatchAction 

注:action扩展的是DispathAction

public   class  DispatchExampleAction  extends  DispathAction {
 

 

struts-config.xml
        
< action  path ="/dispatch-submit"  
                type
="org.apache.struts.webapp.dispatch.DispatchExampleAction"
                parameter
="dispatchMethod"
                name
="testForm"
                scope
="request" >
            
< exception  key ="dispatch.NoSuchMethodException"
                       type
="java.lang.NoSuchMethodException"
                       path
="/dispatch.jsp" />
            
< exception  key ="dispatch.ServletException"
                       type
="javax.servlet.ServletException"
                       path
="/dispatch.jsp" />
            
< forward  name ="success"  path ="/dispatch.jsp" />
        
</ action >

jsp中使用
          
< html:form  action ="dispatch-submit"  style ="display:inline" >
              
< input  type ="hidden"  name ="dispatchMethod"  value ="doFoo"   />
              
< html:submit >< bean:message  key ="button.foo.label"   /></ html:submit >
          
</ html:form >
           //上例调用DispatchExampleAction的doFoo方法,要想调用默认方法可以设置:
              <input type="hidden" name="dispatchMethod" value="execute" />

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 >
              
&nbsp;
              
< html:submit  property ="dispatchParam" >< bean:message  key ="button.bar.label"   /></ html:submit >
              
&nbsp;
              
< html:submit  property ="dispatchParam" >< bean:message  key ="button.invalid.label"   /></ html:submit >
              
&nbsp;
              
< 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方法
 

 
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值