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

  • 相关阅读:
    运用《深入理解Java虚拟机》书中知识解决实际问题
    FPGA实现移动目标检测
    FPGA实现人脸检测
    FPGA实现图像的边缘检测:灰度形态学梯度
    FPGA实现图像的二值形态学滤波:边界提取
    VAST3.0规范
    Flash Socket通信的安全策略问题 843端口
    100个开源游戏
    游戏指标分析
    网络广告类型有哪些?
  • 原文地址:https://www.cnblogs.com/lzhc/p/6490107.html
Copyright © 2011-2022 走看看