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. 关于获得当前的index的方法

    每日一句English(start from today): In the previous section we just displayed a list of string entered st ...

  2. Laravel ORM 数据model操作

    1.ORM操作需要创建对应的model         class User extends Eloquent   2.有两种方式使用数据操作对象            a. 使用new关键字创建对象 ...

  3. Python日志监控系统处理日志(pyinotify)

    前言 最近项目中遇到一个用于监控日志文件的Python包pyinotify,结合自己的项目经验和网上的一些资料总结一下,总的原理是利用pyinotify模块监控日志文件夹,当日志到来的情况下,触发相应 ...

  4. Redis进阶实践之三如何在Windows系统上安装安装Redis

    一.Redis的简介        Redis是一个key-value存储系统.和Memcached类似,它支持存储的value类型相对更多,包括string(字符串).list(链表).set(集合 ...

  5. Oracle 11G Client 安装就可以用上Oracle11G啦,不用傻傻的安装2G多的Oracle啦,安装特别简单,使用起来更简单

    下载地址: http://download.oracle.com/otn/nt/oracle11g/112010/win32_11gR2_client.zip 先将下载下来的ZIP文件解压,并运行se ...

  6. [bzoj2638] 黑白染色

    比较蛋疼的是我们可以先染个底色,再在底色上染别的东西. 由ccz大爷的题解可得..将目标状态里相同颜色的联通块缩点后,枚举起点,生成树里的最大节点深度就是需要的次数了, 如果最大深度是白色的话记得-1 ...

  7. CodeForces776-A.Serial Killer-string

    A Serial Killer time limit per test 2 seconds memory limit per test 256 megabytes input standard inp ...

  8. 2017ecjtu-summer training #3 POJ3264

                                                                                                        ...

  9. hdu_4869(费马小定理+快速幂)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4869 Turn the pokers Time Limit: 2000/1000 MS (Java/O ...

  10. Codeforces Round #280 (Div. 2)_C. Vanya and Exams

    C. Vanya and Exams time limit per test 1 second memory limit per test 256 megabytes input standard i ...