动态方法就是一个Action对应多个请求,减少Action的数量

1、指定method属性

<action name="addAction" method="add" class="com.venn.action.HelloWorldAction">
<result>/jsp/add.jsp</result>
</action>

2、感叹号(!)方式(不推荐使用)

<action name="HelloWorld" class="com.venn.action.HelloWorldAction">
      <result>/jsp/test.jsp</result>
  <result name="add">/jsp/add.jsp</result>
  <result name="update">/jsp/update.jsp</result>
</action>

  需要在struts.xml中加入如下常量:

    <constant name="struts.enable.DynamicMethodInvocation" value="true"></constant>(加在package标签外面)

    调用不同方法使用:

     访问execute方法: http://localhost:8080/TestStruts2/HelloWorld.action

     访问update方法: http://localhost:8080/TestStruts2/HelloWorld!update.action

     访问add方法 http://localhost:8080/TestStruts2/HelloWorld!add.action

3、通配符方式

  Action配置:

<action name="HelloWorld_*" method="{1}" class="com.venn.action.HelloWorldAction">
<result>/jsp/test.jsp</result>
<result name="add">/jsp/add.jsp</result>
<result name="update">/jsp/update.jsp</result>
</action>

    访问execute方法: http://localhost:8080/TestStruts2/HelloWorld.action 或  http://localhost:8080/TestStruts2/HelloWorld_execute.action

    访问update方法: http://localhost:8080/TestStruts2/HelloWorld_update.action

    访问add方法 http://localhost:8080/TestStruts2/HelloWorld_add.action

注:为简化struts.xml配置,可以将action配置为:

<action name="*_*_*" method="{2}" class="com.venn.{3}.{1}Action">
<result>/jsp/test.jsp</result>
<result name="add">/jsp/{2}.jsp</result>
<result name="update">/jsp/{2}.jsp</result>
</action>

第一个*对应action,第二个*对应method

注意result标签的name属性不可以使用通配符

java类

public class HelloWorldAction extends ActionSupport {

@Override
public String execute() throws Exception {
System.out.println("execute method");
return "success";
}

public String add(){
System.err.println("add method");
return "add";
}

public String update(){
System.out.println("update method");
return "update";
}
}

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

  1. Struts2 动态方法调用

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

  2. Struts2动态方法调用(DMI)

    当structs.xml解析到Action的时候,默认执行的是此action的execute()方法,但是实际开发中,我们的action中含有很多方法,比如说增删改查的方法,那么structs.xml ...

  3. (Struts2学习系列三)Struts2动态方法调用:通配符方式

    更改src/struts2.xml的代码: <?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE ...

  4. (Struts2学习系列二)Struts2动态方法调用:指定method属性

    紧接着上一篇,在HelloWorldAction.java中添加add和update方法: public class HelloWorldAction extends ActionSupport{ p ...

  5. 第三篇——Struts2的动态方法调用

    Struts2动态方法调用 默认方式:默认执行方法中的execute方法,若指定类中没有该方法,默认返回success: method方式:执行method属性中定义的方法,没有该方法,页面报错: 通 ...

  6. Struts2学习四----------动态方法调用

    © 版权声明:本文为博主原创文章,转载请注明出处 Struts2动态方法调用 - 默认:默认执行方法中的execute方法,若指定类中没有该方法,默认返回success <package nam ...

  7. struts2DMI(动态方法调用)

    struts2动态方法调用共有三种方式: 1.通过action元素的method属性指定访问该action时运行的方法 <package name="action" exte ...

  8. Struts2学习笔记 - Action篇<动态方法调用>

    有三种方法可以使一个Action处理多个请求 动态方法调用DMI 定义逻辑Acton 在配置文件中使用通配符 这里就说一下Dynamic Method nvocation ,动态方法调用,什么是动态方 ...

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

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

随机推荐

  1. PHP学习笔记之数组篇

    摘要:其实PHP中的数组和JavaScript中的数组很相似,就是一系列键值对的集合.... 转载请注明来源:PHP学习笔记之数组篇   一.如何定义数组:在PHP中创建数组主要有两种方式,下面就让我 ...

  2. Android应用程序基础

    Android程序使用Java语言编写.Android开发套件把数据.资源文件和Java代码编译到一个.以.apk为后缀名的Android压缩包中.一个单独的apk文件中的所有代码被认为是一个andr ...

  3. Spring注解基本解读

    在一个类中使用Spring对象,办法如下: 使用注解的形式注入 从ApplicationContext中获取. T t = new ApplicationContext.getBean("x ...

  4. Mysql删除数据后磁盘空间未释放的解决办法【转】

    转自 Mysql删除数据后,磁盘空间未释放的解决办法 - 今日头条(TouTiao.com)http://toutiao.com/a6303087712678412546/?tt_from=mobil ...

  5. 设置span 宽度的完美解决方案

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...

  6. HDU 3038 How Many Answers Are Wrong 很有意思的一道并查集问题

    题目大意:TT 和 FF玩游戏(名字就值五毛),有一个数列,数列有N个元素,现在给出一系列个区间和该区间内各个元素的和,如果后出现的一行数据和前面一出现的数据有矛盾,则记录下来.求有矛盾数据的数量. ...

  7. 我有一壶酒 Android学习之Service(1)--->BinderService方式

    本文只讨论扩展Binder类 创建一个Binder.xml <?xml version="1.0" encoding="utf-8"?> <L ...

  8. Express静态服务器

    做应用的时候,如果需要给人测试,需要搭建本地服务器: 下载apache tomcat,各种配置,复杂得很. 之前在网上找到python的实现,在文件夹内运行一行代码就可以了,但是可惜,本机访问都很慢. ...

  9. UML类图基本画法

    转自:http://www.cnblogs.com/import-this/p/5330501.html 1. 类 类包括属性方法 下图的生命就是属性 代谢就是方法 2.基本关系 a.泛化(Gener ...

  10. HDU 4456(二维树状数组+坐标转换)

    题目链接:Problem - 4456 看别人叙述看的心烦,于是我自己画了一张图. 上图. 上代码 #include <iostream> #include <cstdio> ...