四种Action详解

四种Action详解
Struts中有四种不同Action,分别如下:
1.Action
2.DispatchAction
3.LookupDispatchAction
4.MappingDispatchAction
关于Action在Struts中的工作流程,这里我就不多说了,只针对各种不同的Action详解
     第一种Action:是最普通、也是我们最先接触的一种,但它仅支持一种业务方法,Action中只能允许有一个execute(),所有比较单一,有局限性,平时的项目中也用的少,或者说,它针对的是一个表单,一个Submit
关于用法:Action中就一个execute(),而strut-config.xml也只需配置一具最普通的<action/>节点,就够了
<!--  Action  -->
    
< action
      attribute
= " loginForm "
      name
= " loginForm "
      path
= " /login "
      scope
= " request "
      type
= " org.svse.struts.action.LoginAction " >
      
< forward name = " ok "  path = " /ok.jsp " ></ forward >
      
< forward name = " error "  path = " /error.jsp " ></ forward >       
      
</ action >
     第二种DispatchAction:这是我们在平时的学习和项目中用的最多的一种,因为它支持多个业务方法的扩展,使用起来也方便,或者说,它针对的是一个表单,多个Button
关于用法:Action中需要几个业务方法,就复制几个execute(),然后改名即可,比如我们做加、减、乘、除,有四个方法,即:add(),sub(),mul(),div();而struts-config.xml中也只需配一个<action/>节点;当然,在涉及到有的业务是否需要表单验证时,可以单独再添加节点嘛
<!--  DispatchAction  -->
    
< action
      attribute
= " calForm "
      name
= " calForm "
      parameter
= " method "
      path
= " /cal "
      scope
= " request "
      type
= " org.svse.struts.action.CalAction " >
      
< forward name = " result "  path = " /cal_result.jsp " ></ forward >
      
</ action >
parameter参数对应的就是我们的业务方法了,在三种DispatchAction中都要用到它,因为有了它,就可实现多业务的调用;
哪我们在视图层,是如何调用的呢?很简单:/cal.do?method=方法名(add、sub)
< script type = " text/javascript " >
        function dosubmit(url)
{
            var myform 
= document.myform;
            myform.action 
= url;
            myform.submit();
        }

    
</ script >
  
</ head >
  
  
< body >
  
<!--  一个表单多个按钮  -->
  
< form method = " post "  name = " myform " >
       第一个数:
< input type = " text "  name = " num1 " >< br >
       第二个数:
< input type = " text "  name = " num2 " >< br >
       
< input type = " button "  value = " "  onclick = " dosubmit('cal.do?method=add') " >
       
< input type = " button "  value = " "  onclick = " dosubmit('cal.do?method=sub') " >
    
< input type = " button "  value = " "  onclick = " dosubmit('cal.do?method=mul') " >
       
< input type = " button "  value = " "  onclick = " dosubmit('cal.do?method=div') " >  
  
</ form >
  
</ body >
     第三种LookupDispatchAction:此种Action恐怕是我们平时用的最少的一种了,尽管它也支持多业务,或者说,它针对的是一个表单,多个Submit;
但用起来非常麻烦,需要struts标签,资源文件,Action中要Map集合;这里就大概说一下
Actoin中业务方法跟上面一样,只不过多一个包含Map的方法:
// 通过资源文件的key(button.add),来找到业务方法add(),sub()
     protected  Map getKeyMethodMap()  {
        Map map 
= new HashMap();
        map.put(
"button.add","add");
        map.put(
"button.sub","sub");
        
return map;
    }
资源文件:
# Resources  for  parameter  ' org.svse.struts.ApplicationResources '
# Project ch03
button.add
= add
button.sub
= sub
struts-config.xml也跟上面一样,根据method来找;
哪视图组件如何来调用呢?
<!--  一个表单多个提交  -->
    
< form action = " cal2.do "  method = " post " >
          num1:
< input type = " text "  name = " num1 " />< br >
          num2:
< input type = " text "  name = " num2 " />< br >
          
< html:submit property = " method " >
              
< bean:message key = " button.add " />
          
</ html:submit >
          
< html:submit property = " method " >
              
< bean:message key = " button.sub " />
          
</ html:submit >
    
< form >
怎么样够麻烦的吧。。。
     第四种MappingDispatchAction:我自认为它比DispatchAction差不多好用,只是它够灵活(这里针对<action/>节点,想咋配咋配),或者说,它针对的是多个表单,多个Submit;
关于用法,也比较简单:
Action中的业务实现跟DispatchAction一样,strut-config.xml则要灵活些,针对每一个业务方法,配一个<action/>节点:
<!--  MappingDispatchAction  -->
    
<!--  add  -->
    
< action
      attribute
= " calForm "
      name
= " calForm "
      parameter
= " add "
      path
= " /add "
      scope
= " request "
      type
= " org.svse.struts.action.Cal3Action " >
      
< forward name = " result "  path = " /cal_result.jsp "   />
    
</ action >
    
<!--  sub  -->
    
< action
      attribute
= " calForm "
      name
= " calForm "
      parameter
= " sub "
      path
= " /sub "
      scope
= " request "
      type
= " org.svse.struts.action.Cal3Action " >
      
< forward name = " result "  path = " /cal_result.jsp "   />
    
</ action >
视图组件的调用就很能够方便了:直接跟/path路径名.do:
< form action = " add.do "  method = " post " >
        num1:
< input type = " text "  name = " num1 " >< br >
        num2:
< input type = " text "  name = " num2 " >< br >
        
< input type = " submit "  value = " " >
    
</ form >
Struts中四种Action用法基本完毕,此纯属本人在学习过程中自己的总结,若有不当之处,愿不令赐教!!!

你可能感兴趣的:(四种Action详解)