动态方法调用-深入Struts2

一 提出背景
动态方法调用是为了解决一个Action对应多个请求的处理,以免Action太多。
 
二 解决方法


 
 
三 方案1——指定method属性
1、Action编写
package com.cakin.action;
 
import com.opensymphony.xwork2.ActionSupport;
 
public class HelloWorldAction extends ActionSupport {
         @Override
         public String execute() throws Exception {
                System. out .println( "执行Action" );
                 return SUCCESS ;
        }
        
         public String add()
        {
                 return SUCCESS ;
        }
        
         public String update()
        {
                 return SUCCESS ;
        }
}
2、Struts编写
<? xml version = "1.0" encoding = "UTF-8" ?>
<! DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.1//EN" " http://struts.apache.org/dtds/struts-2.1.dtd" ; >
< struts >
         < package name = "default" namespace = "/" extends = "struts-default" >
                 < action name = "helloworld" class = "com.cakin.action.HelloWorldAction" >
                         < result > /result.jsp </ result >
                 </ action >
                
                 < action name = "addAction" method = "add" class = "com.cakin.action.HelloWorldAction" >
                         < result > /add.jsp </ result >
                 </ action >
                 < action name = "updateAction" method = "update" class = "com.cakin.action.HelloWorldAction" >
                         < result > /update.jsp </ result >
                 </ action >
         </ package >
</ struts >    
3、测试


 
 
四 方案2——感叹号方式
1、Action编写
package com.cakin.action;
 
import com.opensymphony.xwork2.ActionSupport;
 
public class HelloWorldAction extends ActionSupport {
         @Override
         public String execute() throws Exception {
                System. out .println( "执行Action" );
                 return SUCCESS ;
        }
        
         public String add()
        {
                 return "add" ;
        }
        
         public String update()
        {
                 return "update" ;
        }
}
2、Struts编写
<? xml version = "1.0" encoding = "UTF-8" ?>
<! DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.1//EN" " http://struts.apache.org/dtds/struts-2.1.dtd" ; >
< struts >
         < package name = "default" namespace = "/" extends = "struts-default" >
                 < action name = "helloworld" class = "com.cakin.action.HelloWorldAction" >
                         < result > /result.jsp </ result >
                         < result name = "add" > /add.jsp </ result >
                         < result name = "update" > /update.jsp </ result >
                 </ action >
         </ package >
        
         < constant name = "struts.enable.DynamicMethodInvocation" value = "true" ></ constant >
</ struts >    
3、测试


 
 
五 方案3——单个通配符方式
1、Action编写
package com.cakin.action;
 
import com.opensymphony.xwork2.ActionSupport;
 
public class HelloWorldAction extends ActionSupport {
         @Override
         public String execute() throws Exception {
                System. out .println( "执行Action" );
                 return SUCCESS ;
        }
        
         public String add()
        {
                 return "add" ;
        }
        
         public String update()
        {
                 return "update" ;
        }
}
2、Struts编写
<? xml version = "1.0" encoding = "UTF-8" ?>
<! DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.1//EN" " http://struts.apache.org/dtds/struts-2.1.dtd" ; >
< struts >
         < package name = "default" namespace = "/" extends = "struts-default" >
                 < action name = "helloworld_*" method = "{1}" class = "com.cakin.action.HelloWorldAction" >
                         < result > /result.jsp </ result >
                         < result name = "add" > /{1}. jsp </ result >
                         < result name = "update" > /{1}. jsp </ result >
                 </ action >
         </ package >
        
         < constant name = "struts.enable.DynamicMethodInvocation" value = "false" ></ constant >
</ struts >    
3、测试


 
 
六 方案3——多个通配符方式
1、Action编写
package com.cakin.action;
 
import com.opensymphony.xwork2.ActionSupport;
 
public class HelloWorldAction extends ActionSupport {
         @Override
         public String execute() throws Exception {
                System. out .println( "执行Action" );
                 return SUCCESS ;
        }
        
         public String add()
        {
                 return "add" ;
        }
        
         public String update()
        {
                 return "update" ;
        }
}
2、Struts编写
<? xml version = "1.0" encoding = "UTF-8" ?>
<! DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.1//EN" " http://struts.apache.org/dtds/struts-2.1.dtd" ; >
< struts >
         < package name = "default" namespace = "/" extends = "struts-default" >
                 < action name = "*_*" method = "{2}" class = "com.cakin.action.{1}Action" >
                         < result > /result.jsp </ result >
                         < result name = "add" > /{2}. jsp </ result >
                         < result name = "update" > /{2}. jsp </ result >
                 </ action >
         </ package >
        
         < constant name = "struts.enable.DynamicMethodInvocation" value = "false" ></ constant >
</ struts >    
3、测试


 

猜你喜欢

转载自cakin24.iteye.com/blog/2397138