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

  • 相关阅读:
    Shiro结合配置文件实战实现权限验证
    Shiro的一些概念解释以及配置了解
    jedis工具类
    【转载】redis的主从复制
    Redis的事务
    redis的持久化
    SSM框架-SpringMVC 实例文件上传下载
    如何设置CentOS 7获取动态及静态IP地址
    阿里云linux centos 一键部署web环境--图文详解
    SVN使用教程总结
  • 原文地址:https://www.cnblogs.com/lzhc/p/6490107.html
Copyright © 2011-2022 走看看