动态方法就是一个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. opencv 相关一个很好的博客

    http://blog.csdn.net/zouxy09/article/category/1218765 图像卷积与滤波的一些知识点 图像卷积与滤波的一些知识点zouxy09@qq.comhttp: ...

  2. copy_part_stat.sql

    spool 04_copy_part_stat.log conn / as sysdba execute sys.dbms_stats.export_schema_stats(ownname=> ...

  3. Android PopupWindow的使用技巧(转)

    Android PopupWindow的使用技巧 PopupWindow是Android上自定义弹出窗口,使用起来很方便. PopupWindow的构造函数为 public PopupWindow(V ...

  4. hrbustoj 2013 Play Game 2(博弈)

    注释在代码里 /* 1.若输入2 ~ 9 ,因为Stan 是先手,所以Stan 必胜 2.若输入10~18 ,因为Ollie 是后手,不管第一次Stan 乘的是什么,Stan肯定在 2 ~ 9 之间, ...

  5. ZUFE OJ 2301 GW I (3)

    Description GW 是ZUFE的神犇,有一天他想到一种神奇的变换,并且将它命名为GW变换 对于一个数字n,该变换后的值GW(n)为,先令X=n 第一步,如果X为个位数,GW(n)=X,否则执 ...

  6. webapp之路--理解viewport的使用

    随着高端手机(Andriod,Iphone,Ipod,WinPhone等)的盛行,移动互联应用开发也越来越受到人们的重视,用html5开发移动应用是最好的选择.然而,每一款手机有不同的分辨率,不同屏幕 ...

  7. 关于NIOS ii烧写的几种方式

    1. 方法一:.sof和.elf全部保存在FPGA内,程序加载和运行也是在FPGA内部. 把FPGA的配置文件.sof通过JTAG方式下载(其实是在线运行)进入FPGA本身,此时在NIOS II的界面 ...

  8. wget获取https资源

    使用wget获取 https资源,缺省命令下是要使用证书,如果还未安装证书,可以选择忽略. 例如没有github.com的证书,执行如下命令 mkdir -p model cd model wget ...

  9. CodeForces 616A Comparing Two Long Integers

    水题 #include<cstdio> #include<cstring> #include<algorithm> using namespace std; +; ...

  10. margin 相关 bug 系列

    原文地址:margin系列之bug巡演 by @doyoe IE6浮动双倍margin bug 这当是IE6最为经典的bug之一. 触发方式 元素被设置浮动 元素在与浮动一致的方向上设置margin值 ...