动态方法调用-深入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、测试

 



 
  • 大小: 87.4 KB
  • 大小: 18.5 KB
  • 大小: 18.6 KB
  • 大小: 19.6 KB
  • 大小: 18.7 KB
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值