struts中action类继承了ActionSupport  默认实现了execute()方法

struts.xml配置文件中

然后可以配置如下映射:

<package name ="ActionDemo" extends ="struts-default">

<action name ="HelloWorld" class ="tutorial.HelloWorld">

<result> /HelloWorld.jsp</result>

</action>

</package>

public class TestAction extends ActionSupport {

private static final long serialVersionUID = 1L;
    private String helo;
    private String hehe;
    private String message;

public String aliasAction() {  
        setMessage("自定义Action调用方法");  
        return SUCCESS;  
    }
    
    public String getHelo() {
        return helo;
    }

public void setHelo(String helo) {
        this.helo = helo;
    }

@Override
    public String execute() throws Exception {
        helo = "hello,world";
        hehe = "haha";
        return SUCCESS;
    }

public void setHehe(String hehe) {
        this.hehe = hehe;
    }

public String getHehe() {
        return hehe;
    }

public void setMessage(String message) {
        this.message = message;
    }

public String getMessage() {
        return message;
    }
}

在默认情况下,当请求HelloWorld.do发生时,Struts 2会根据struts.xml里的Action映射集(Mapping)实例化tutoiral.HelloWorld类,并调用其execute()方法。当然,我们可以通过以下两种方法改变这种默认调用,这个功能(Feature)有点类似Struts 1中的LookupDispathAction。

在sturts.xml中新建Action,并指明其调用的方法。

访问Action时,在Action名后加上"!xxx"(xxx为方法名)。

然后可以在sturts.xml中指定method来设置请求的函数名:

<action name="AliasHelloWorld" class="  
    tutorial.HelloWorld" method="aliasAction">  
       <result>/HelloWorld.jsp</result>  
    </action>

也可以直接在URL地址栏中使用"!method"来设置请求的函数名:

http://localhost:8080/Struts 2Test  
    /HelloWorld!aliasAction.action

上面为第一种方法是通过在struts.xml文件中对应的action声明method,然后在对应的action中写对应的方法。

下面介绍第二种方式,大同小异

<struts>

<package name="default" namespace="/" extends="struts-default">
      
        <action name="hello" class="com.action.TestAction" method="aliasAction">
            <result>/success.jsp</result>
            <result name="add">/add.jsp</result>
            <result name="update">/update.jsp</result>
        </action>
    </package>
    <constant name="struts.enable.DynamicMethodInvocation" value="true"></constant>
</struts>

TestAction的action为

public class TestAction extends ActionSupport {

private static final long serialVersionUID = 1L;
    
    public String add(){
        
        return "add";
    }
    
    public String update(){
        return "update";
    }

public String aliasAction() {  
        setMessage("自定义Action调用方法");  
        return SUCCESS;  
    }
    
    public String getHelo() {
        return helo;
    }

public void setHelo(String helo) {
        this.helo = helo;
    }

@Override
    public String execute() throws Exception {
        helo = "hello,world";
        hehe = "haha";
        return SUCCESS;
    }

public void setMessage(String message) {
        this.message = message;
    }

public String getMessage() {
        return message;
    }
}

贴出请求:http://admin-pc:8080/StrutsDemo/hello!update.action

第二个与第一个的异同:都声明了方法,第一个声明的方法的返回值为SUCCESS,第二个声明的方法的返回值为返回的为struts.xml中

<result name="add">/add.jsp</result> 的name相同。

 第二个struts.xml中必须有    <constant name="struts.enable.DynamicMethodInvocation" value="true"></constant>方可使用

struts 中自定义action访问方法的更多相关文章

  1. Swift Tips - 在 Swift 中自定义下标访问

    Untitled Document.md input[type="date"].form-control,.input-group-sm>input[type="d ...

  2. Struts中的 saveToken的方法

    Struts中的 saveToken的方法     saveToken防止2次提交的问题 struts有自带的Token(令牌)的机制来解决重复提交(包括后退,刷新等).举例: 假设:假设有一个新增用 ...

  3. jquery遍历标签中自定义的属性方法

    在开发中我们有时会对html标签添加属性,如何遍历处理 <ul> <li name="li1" sortid="nav_1">aaaaa ...

  4. MFC中 自定义类访问主对话框控件的方法

    之前一直在找有木有好点的方法.现在终于被我找到,收藏之~~~~~~ 在使用mfc的时候经常遇到自定义类访问主对话框控件的问题,例如自定义类中的方法要输出一段字符串到主对话框的EDIT控件.控制对话框的 ...

  5. 五 Action访问方法,method配置,通配符(常用),动态

    1 通过method配置(有点low) 建立前端JSP:demo4.jsp <%@ page language="java" contentType="text/h ...

  6. struts开发&lt;struts中的action详细配置. 二&gt;

    在eclipse中新建项目StrutsDemo1[struts的配置见]struts开发<在eclipse中配置struts. 一> 详细文件夹结构例如以下 第一种配置方法 新建UserA ...

  7. struts2中的action访问web对象

    Struts2的Action就是一个普通的POJO对象,它和Web对象request.response.session和application没有耦合在一起,这样便于单独测试Action,那么我们在A ...

  8. 详解struts2中配置action的方法

    如何解决action太多的问题??我们因为需要不同的方法,所以往往建立很多不同的类,但是每个类中往往仅仅几行代码,不仅浪费了时间,而且配置起来也很繁琐,所以,建立一个共有的类,然后根据以下方式来操作, ...

  9. Swift中自定义Log打印方法

    系统如何调用super方法 系统默认只会在构造函数中,自动调用super.init()方法,而且是在所写方法的尾部进行调用. 在其他函数中,如何需要调用父类的默认实现,都需要手动去实现. 如果在构造函 ...

随机推荐

  1. Repair MySQL 5.6 GTID replication by injecting empty transactions

    Since SQL_SLAVE_SKIP_COUNTER doesn’t work with GTID we need to find a way to ignore that transaction ...

  2. HashTable源码阅读

    环境jdk1.8.0_121 与HashMap有几点区别 在HashMap中,冲突的值会在bucket形成链表,当达到8个,会形成红黑树,而在HashTable中,冲突的值就以链表的形式存储 publ ...

  3. jq传统火车轮播图

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  4. 请求库-selenium 模块

    # -*- coding: utf-8 -*- # 安装:pip3 install selenium # 下载chromdriver.exe放到python安装路径的scripts目录中即可,注意最新 ...

  5. 【转】TCP粘包分析

    一 .两个简单概念长连接与短连接: 1.长连接     Client方与Server方先建立通讯连接,连接建立后不断开, 然后再进行报文发送和接收. 2.短连接     Client方与Server每 ...

  6. CSS3让长单词与URL地址自动换行——word-wrap属性

    div{ word-wrap:break-word; } word-wrap属性可以使用的属性值为normal与break-word两个.使用normal属性值时浏览器默认处理,只在半角空格或者连字符 ...

  7. HDU 1312 Red and Black(DFS,板子题,详解,零基础教你代码实现DFS)

    Red and Black Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) To ...

  8. 2017广东工业大学程序设计竞赛初赛 题解&源码(A,水 B,数学 C,二分 D,枚举 E,dp F,思维题 G,字符串处理 H,枚举)

    Problem A: An easy problem Description Peter Manson owned a small house in an obscure street. It was ...

  9. HDU 5752 Sqrt Bo【枚举,大水题】

    Sqrt Bo Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others)Total S ...

  10. java web开发 高并发处理

    转自:http://blog.csdn.net/zhangzeyuaaa/article/details/44542161 java处理高并发高负载类网站中数据库的设计方法(java教程,java处理 ...