JavaWeb-struts2动态方法调用

struts2动态方法调用 DynamicMethodInvoke
默认的"struts.xml"配置信息如下:
<action name="hello" class="cn.lzh.struts2.action.HelloWorldAction" method="execute">
<result name="success">/HelloWorld.jsp</result>
</action>
想用动态方法调用时,在配置时,不用指定“method”,每个方法对就的“result”要写上 
<action name="hello" class="cn.lzh.struts2.action.HelloWorldAction" >
<result name="addInput">/addInput.jsp</result>
<result name="updateInput">/updateInput.jsp</result>
</action>


相应的请求方法:
在请求地址后加查询字符串属性指定调用方法的名称,格式如下: method:[调用方法名]
1.0>在浏览器上用默认调用方式时:
  报错:java.lang.NoSuchMethodException: ......execute() 
        // 默认不指定或没写“method”时,系统会自动调用method="execute"
1.1> 手工在浏览器中上调用指定方法:
    http://localhost:8080/web/test/simple.action?method:addInput


1.2> 在网页中表单的<form action="/web/test/simple.action?method:addInput" methon="post" />
1.3> 在网页中表单中使用隐藏域 <input type="hidden" name="method:addInput" />




在请求地址加 "![调用方法名]"
2.1> http://localhost:8080/web/test/simple!addInput.action
2.2> 同理在网页中表单的<form action="/web/test/simple!addInput.action" methon="post" />


在配置文件中使用通配符(其中的"_"是分隔符,可以连接多个参数)
<action name="simple_*" class="cn.lzh.struts2.action.SimpleAction"  methon="{1}">
<result name="addInput">/addInput.jsp</result>
<result name="updateInput">/updateInput.jsp</result>

</action>






Struts2-action请求的方法调用:
动态方法调用就是为了解决一个Action对应多个请求的处理,以免Action太多
1>指定method属性
  在struts.xml配置文件中配置Action的时候用method=" " 来指定执行的哪个方法。
  请求方式:
  1.1> http://localhost:8080/actionName?method:mothodName
  1.2> <form action="actionName" methond="pose" >
           <input type="hidden" name="method:mothodName">
  ...
       </form>


2>感叹号方式(需要开启),官网不推荐.
  用这种方式需要先开启一个开关,将此常量设置为true,这种方式才能使用
  <constant name="struts.enable.DynamicMethodInvocation" value="true" /> 
  链接请求时指定<a href="actionName!mothodName">result1</a>
  也可以在url地址中动态指定action执行哪个方法,可以通过action+!+方法名。
  (注:只要Action对象中有这个方法,并且返回类型为String就可以调用,这样struts.xml文件配置文件就可以不需要配置method属性)
  请求方式:
  2.1> http://localhost:8080/actionName!mothodName
  2.2> <form action="actionName!mothodName" method="pose" > ... </form>


3>第三种方式:下划线分隔的"*"通配符方式(官网推荐使用)
  首先得关闭开关
  <constant name="struts.enable.DynamicMethodInvocation" value="false" /> 
  请求方式:
  <action name="actionName_*" class="action_class" method="{1}">
      <result name="test1">/…/test1.jsp</result>
      <result name="test2">/…/test2.jsp</result>
  </action> 
  注: actionName_*这里的*就是你呆会要匹配的字符串,匹配*号时用大括号
  请求方式:
  3.1> http://localhost:8080/actionName_mothodName
  3.2> <form action="actionName_mothodName" methond="pose" > ... </form>









































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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值