Struts2中DMI(动态方法调用)

 1  <package name="front" namespace="/front" extends="struts-default">
 2         <default-action-ref name="index" />
 3         <action name="helloword" class="struts.IndexAction">
 4             <result name="add">
 5                /hello.jsp
 6             </result>
 7             <result name="love">
 8                 /love.jsp
 9             </result>
10         </action>
11 </package>

大家看上面程序,指定了action的class="struts.IndexAction“

再来看IndexAction类

 1 package struts;
 2 import com.opensymphony.xwork2.ActionSupport;
 3 
 4 
 5 public class IndexAction extends ActionSupport{
 6     public String add(){
 7      return "add";  //返回的result名称
 8     }
 9     public String love(){
10      
11      return "love";
12     }
13 }

里面并没有excute()方法,这时大家给以这样配置

  <action name="helloword" class="struts.IndexAction" method="add">

  就可以返回IndexAction类中add方法的值,

但是这种方法不推荐!推荐的方法是动态调用,也就是DMI.

比如在地址栏中输入URL:http://localhost:8080/struts2/front/helloword!add (!后面的add是调用的方法名, 默认调用execute())

但是:如果这样输入的话,会报错(There is no Action mapped for namespace [/front] and action name [helloword!add()] associated with context path [/Struts2_10003].)
因为:struts2中默认不允许使用DMI

所以:需要在配置文件中打开: <constant name="struts.enable.DynamicMethodInvocation" value="true"/>这样大家在地址栏动态输入就可以得到预期的页面

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值