zoukankan      html  css  js  c++  java
  • struts2学习笔记(2)action多个方法的动态调用

    ①在struts.xml中的action添加method

      <action name="addhelloworld" method="add" class="com.lzhc.action.HelloWorldAction">
        <result>/add.jsp</result>
      </action>

      在src中的com.lzhc.action包下的HelloWorldAction中添加add方法如下

      public class HelloWorldAction extends ActionSupport {

        public String add(){

          return SUCCESS;

        }

        public String update(){

          return SUCCESS;

        }

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

      }

    即在函数中包含多个方法时,可以分别用method引用。由于每个方法都需要改写一个对应的配置文件,比较繁琐

    ②用感叹号的方法

      在struts.xml中添加以下代码

      <constant name="struts.enable.DynamicMethodInvocation" value="true"></constant>

      在action中即可添加任意方法,如:

      <action name="helloworld" class="com.lzhc.action.HelloWorldAction">
        <result>/result.jsp</result>
        <result name="add">/add.jsp</result>
        <result name="update">/update.jsp</result>
      </action>

      在src中的com.lzhc.action包下的函数HelloWorldAction中添加以下方法:

      public String add(){
        return "add";
      }

      public String update(){
        return "update";
      }

    再在webroot下添加add.jsp和update.jsp两个jsp文件,此时若要访问add方法,只需将地址写为:

    http://localhost:8080/test/helloworld!add.action

    ③最为常用的是通配符的方式

        优点:只用配置一次struts.xml,只要按约定命名,就无需再改动

        <action name="helloworld_*" class="com.lzhc.action.HelloWorldAction" method="{1}">

          <result name="add">/{1}.jsp</result>

          <result name="update">/{1}.jsp</result>

        </action>

        *可任意匹配,{1}为方法名

        访问页面为http://localhost:8080/test/helloworld_add.action

        或直接写成:

        <action name="*_*" class="com.lzhc.action.{1}Action" method="{2}">

          <result name="add">/{2}.jsp</result>

          <result name="update">/{2}.jsp</result>

        </action>

        此时访问网页为http://localhost:8080/test/HelloWorld_add.action

        即http://localhost:8080/文件名/{1}_{2}.action

  • 相关阅读:
    杂货铺
    oracle修改已存在数据的字段类型
    使用python读取配置文件并从mysql数据库中获取数据进行传参(基于Httprunner)
    HttpRunner完整笔记(从搭建到应用)
    使用jmeter发送put请求的三种方式
    电脑同时安装了python2和python3后,随意切换版本并使用pip安装
    python+request+HTMLTestRunner+unittest接口自动化测试框架
    redis简介
    spring cloud gateway之服务注册与发现
    spring cloud gateway 之限流篇
  • 原文地址:https://www.cnblogs.com/lzhc/p/6490107.html
Copyright © 2011-2022 走看看