zoukankan      html  css  js  c++  java
  • Struts2动态方法调用

      动态方法就是一个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";
    }
    }

  • 相关阅读:
    接口自动化测试中解决所遇问题的博客链接
    python中logging日志模块详解
    yaml.load()时总是出现警告:YAMLLoadWarning: calling yaml.load() without Loader=...
    基于ArcGIS Desktop 10.2开发的环境安装
    【部署】IIS导入证书后绑定报错“证书中的一个或多个中间证书丢失”
    IIS7.x 生成CSR证书请求文件
    Sql Server多种分页性能的比较
    网页上出现D盾拦截,删除、取消
    Fusioncharts图表常用参数设置
    Windows Server 2016 安装.NET Framework 3.5 错误
  • 原文地址:https://www.cnblogs.com/Springmoon-venn/p/5578965.html
Copyright © 2011-2022 走看看