动态方法就是一个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. SoftReference 介绍

    1 Java中的SoftReference 2 即对象的软引用.如果一个对象具有软引用,内存空间足够,垃 圾回收器就不会回收它:如果内存空间不足了,就会回收这些对象的内存.只要垃圾回收器没有回收它,该 ...

  2. Android蓝牙传感应用(转)

    源:http://www.cnblogs.com/xiaochao1234/p/3753538.html Android手机一般以客户端的角色主动连接SPP协议设备(接上蓝牙模块的数字传感器),连接流 ...

  3. 自己写的angularJs排序指令【原创】

    首先,给大家看看指令完成的效果.(请注意区分下面几张图片中,对象值的变化) 好了,效果图已经看完了,如果大家有兴趣的话可以继续往下看,接下来要讲的是关于angularJs中filter内置好的orde ...

  4. 在Window平台下安装xgboost的Python版本

    原文:http://blog.csdn.net/pengyulong/article/details/50515916 原文修改了两个地方才安装成功,第3步可以不用,第2步重新生成所有的就行了. 第4 ...

  5. web 开发:CSS3 常用属性——速查手册!

    web 开发:CSS3 常用属性——速查手册! CSS3 简介:http://www.runoob.com/css3/css3-intro.html 1.目录 http://caniuse.com/ ...

  6. 百度网盘API的操作--PCS 百度个人云存储 上传 ,下载文件

    来自http://blog.csdn.net/u014492257/article/details/39856403 另外需要所有API使用方法的请访问本人上传的资源(需要3个下载分的)链接: htt ...

  7. json转义字符串

    json前台写数据 @RequestMapping("/addUserJson") public void addUserJson(User user,HttpServletReq ...

  8. how to enable #ifdef macro in the command line of make?

    Compilers normally use the -D flags eg Code: test.o: test.cpp $(CC) $(CFLAGS) -DTEST1 test.cpp -o $@

  9. Java从键盘输入

    package my;import java.util.Scanner; public class MyJava { /**     * @param args     */    public st ...

  10. Ext中包含了几个以get开头的方法

    Ext中包含了几个以get开头的方法,这些方法可以用来得到文档中DOM.得到当前文档中的组件.得到Ext元素等,在使用中要注意区别使用.1.get方法get方法用来得到一个Ext元素,也就是类型为Ex ...