Struts2中动态方法调用就是为了解决一个action对应多个请求的处理,以免action太多。

  主要有一下三种方法:指定method属性、感叹号方式和通配符方式。推荐使用第三种方式。

  1.指定method属性

    LoginAction.java

public class LoginAction extends ActionSupport{
public String execute (){ return "success" ;
}
public String add(){
return "add" ;
}
public String update(){
return "update" ;
}
}

    struts.xml,配置多个action标签。

<struts >
<package name= "default" namespace ="/" extends="struts-default" >
<action name= "LoginAction" class="com.struts2.action.LoginAction" method ="execute" >
<result name= "success">/success.jsp </result>
</action>
<action name= "addAction" class="com.struts2.action.LoginAction" method ="add" >
<result name= "add">/add.jsp </result>
</action>
<action name= "updateAction" class="com.struts2.action.LoginAction" method ="update" >
<result name= "update">/update.jsp </result>
</action>
</package>
</struts >

   当访问路径为http://localhost:8088/jspToAction1/LoginAction.action时,跳转到success.jsp

   当访问路径为http://localhost:8088/jspToAction1/addAction.action时,跳转到add.jsp

     当访问路径为http://localhost:8088/jspToAction1/updateAction.action时,跳转到update.jsp

  

  2.感叹号方式

  LoginAction.java不变如1所示。

  struts.xml

  struts.enable.DynamicMethodInvocation = true 表示动态方法调用

<struts >
<package name= "default" namespace ="/" extends="struts-default" >
<action name= "LoginAction" class="com.struts2.action.LoginAction" method ="execute" >
<result name= "success">/success.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 >

  

  访问链接时:http://localhost:8080/struts2Demo/LoginAction!update.action,跳转到update.jsp

  访问链接时:http://localhost:8080/struts2Demo/LoginAction!add.action,跳转到add.jsp

  3.通配符方式(最常用的方式也是struts官方推荐的方式)

  LoginAction.java同样不变,struts.xml改为如下:

<struts >
<package name= "default" namespace ="/" extends="struts-default" >
<!-- {1}通过*的方式传递进来 -->
<action name= "LoginAction_*" class="com.struts2.action.LoginAction" method ="{1}" >
<result name= "success">/{1}.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 >
  "LoginAction_*"中的“*”就代表{1},通过链接的方式进行匹配、传值,从而达到一个动态访问的效果。如果name=""中有多个”*“,则第一个用{1}来表示,第二个有{2}来表示,以此类推。
  访问链接时: http://localhost:8088/jspToAction1/HelloWorld_add.action 跳转到add.jsp
  访问链接时: http://localhost:8088/jspToAction1/HelloWorld_update.action   跳转到update.jsp
  

**************************************************************************************************

  如若转载请注明出处,谢谢。By奋斗的小蘑菇

Struts2中动态方法的调用的更多相关文章

  1. Action中动态方法的调用 Action中通配符的使用 Result的配置

       Action中动态方法的调用 动态方法调用(Dynamic Method Invocation,DMI) 标识符:! 一.通过以下选中的文件来查看是否禁止调用动态方法

  2. struts2中的方法的调用

    转载:http://blog.csdn.net/hephec/article/details/41808585 在Struts2中方法调用概括起来主要有三种形式: 第一种方式:指定method属性 & ...

  3. 第三章Struts2 Action中动态方法调用、通配符的使用

    01.Struts 2基本结构 使用Struts2框架实现用登录的功能,使用struts2标签和ognl表达式简化了试图的开发,并且利用struts2提供的特性对输入的数据进行验证,以及访问Servl ...

  4. Struts2 Action中动态方法调用、通配符的使用

    一.Struts2执行过程图: 二.struts2配置文件的加载顺序 struts-default.xml---struts-plugin.xml---struts.xml 具体步骤: 三.Actio ...

  5. Struts2的动态方法,及result跳转方式,全局结果以及默认的action的配置

    Action动态方法的调用 首先我们需要在struts.xml中去配置一个常量值如下 那么去哪找呢?找到Struts-core.jar并打开 method属性 <action name=&quo ...

  6. 分享知识-快乐自己:Struts2(动态方法、动态结果、通配符、方法内部跳转、action跳转、OGNL 的使用及注意事项)

    这里主要以案例演示:涵盖以下知识点 1.动态方法调用:使用关键 符号 ! 进行操作   例如:/命名空间 ! 方法名 2.动态结果集:就是说,通过后台返回的结果 ${} 获取,重定向到符合预期的页面. ...

  7. InvocationHandler中invoke()方法的调用问题

    转InvocationHandler中invoke()方法的调用问题 Java中动态代理的实现,关键就是这两个东西:Proxy.InvocationHandler,下面从InvocationHandl ...

  8. 分析spring事务@Transactional注解在同一个类中的方法之间调用不生效的原因及解决方案

    问题: 在Spring管理的项目中,方法A使用了Transactional注解,试图实现事务性.但当同一个class中的方法B调用方法A时,会发现方法A中的异常不再导致回滚,也即事务失效了. 当这个方 ...

  9. struts2.3.15.3中动态方法调用默认是关闭的

    初学ssh,用的struts2.3.15.3,使用了如下表单: <form action="/spring3/index/login.action" method=" ...

随机推荐

  1. gulp 基本使用

    1, gulp 依赖node, 使用gulp 之前,要先安装node.  Node 安装完成后,它自带npm. Npm: node package manager 就是node 包管理器. 用过jav ...

  2. appium 等待方法 转

    前些日子,配置好了appium测试环境,至于环境怎么搭建,参考:http://www.cnblogs.com/tobecrazy/p/4562199.html   知乎Android客户端登陆:htt ...

  3. PHP data

  4. Javascript优化后的加减乘除(解决js浮点数计算bug)

    function add(a, b) { var c, d, e; try { c = a.toString().split(".")[1].length; } catch (f) ...

  5. Apache Commons BeanUtils

    http://commons.apache.org/proper/commons-beanutils/javadocs/v1.9.2/apidocs/org/apache/commons/beanut ...

  6. js中this的使用

    this是Javascript语言的一个关键字. 它代表函数运行时,自动生成的一个内部对象,只能在函数内部使用.比如, function test(){ this.x = 1; } 随着函数使用场合的 ...

  7. Ubuntu: ImportError: No module named xgboost

    ImportError: No module named xgboost 解决办法: git clone --recursive https://github.com/dmlc/xgboost cd ...

  8. jQuery 点击显示再次点击隐藏

    <html> <head> <script type="text/javascript" src="/jquery/jquery.js&qu ...

  9. css中的一些属性解析

    1.inline-block 存在问题:inline-block的相互间距,元素之间会有一个左右2px的margin一样产生            请看中间的空隙. 为什么会产生这个空隙呢?? 怎么解 ...

  10. 解决ViewPager多次刷新后重叠问题

    @Override public void destroyItem(ViewGroup container, int position, Object object) { ((ViewPager) c ...