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. web 打开子窗口提交数据或其他操作后 关闭子窗口且刷新父窗口实现

    父页面 : html连接:<a href="javascript:void(0)" onclick="window.open(子页面URL)">js ...

  2. Sql Server中实现Mysql中的group_concat函数效果

    ), GuestName) , , '') as CustomerName FROM orderitem oi 以上涉及的两个表是OrderItem和Guest,以属性OrderSN和ItemId连接 ...

  3. RDLC中添加参数,用来显示报表中数据集之外的信息。

    我添加了两个参数,首先后台: ReportParameter rp = ,,).ToString()); ReportParameter rp1 = new ReportParameter(" ...

  4. 在myeclipse2014使用git上传github

    简介 首先在myeclipse中安装github客户端插件,这里就不说了,跟安装svn客户端插件一样的步骤 1.选中要push到github的工程右键team->share project-&g ...

  5. Jekyll x Liquid 控制文章列表只显示特定类别的Post

    使用Liquid按照Category或者Tag过滤Post List 文章首发于szhshp的第三边境研究所(szhshp.org), 转载请注明 前段时间画了一些漫画,考虑把漫画相关的Post放到另 ...

  6. ps切图抠图详解-web前端(转)

    网页设计在技术层面上,第一步是美工做出网页效果图,第二步就是网页前端进行网页切图.网页切图工具常用的有fireworks.PS,这里使用PS进行网页切图. 我们通过设计稿,得到我们想要的产出物(如.p ...

  7. 小tip:关于typeof,instanceof,toString(),valueOf(),toLocaleString(),join(),reverse(),sort(),pop(),push(),shift(),unshift()

    typeof:用于检测一个变量是否是基本数据类型.instanceof用于检测某引用对象是什么类型的对象. var s = "Nicho"; var b = true; var n ...

  8. 289. Game of Life -- In-place计算游戏的下一个状态

    According to the Wikipedia's article: "The Game of Life, also known simply as Life, is a cellul ...

  9. postgresql Delete+ join

    using tablename as alias DELETE FROM tv_episodes USING tv_episodes AS ed LEFT OUTER JOIN data AS nd ...

  10. 初识ReactJs(一)

    React的开发背景 ReactJS官网地址:http://facebook.github.io/react/ Github地址:https://github.com/facebook/react J ...