zoukankan      html  css  js  c++  java
  • ActionSupport里面有execute

    问题:

    action继承了ActionSupport,没有重载execute()方法,也没有用method指定方法,这样行吗。

    我看书上说是要在struts.xml中要用method指定方法,但是我没有指定,程序好像还是正确的被访问到了,不知道为什么?

    A:
    action继承了ActionSupport,而在struts2里已经默认指定了,如果你没有在struts.xml中指定method,那么系统就会调用execute()。而因为你的action继承了ActionSupport,ActionSupport里面有execute方法默认返回success.所以程序还是能被正常执行
     
     
    背景知识补充:
     
    Q:struts做开发的时候action是继承Action方法还是ActionSupport方法?
     
    A:我做的时候,我的action是继承ActionSupport的
    Struts 2的Action无须实现任何接口或继承任何类型,普通的POJO类就可以用做Action类,但是,我们为了方便实现Action,大多数情况下都会继承com.opensymphony.xwork2.ActionSupport类,并重载(Override)此类里的String execute()方法
    因为ActionSupport已经实现了Action接口,还实现了Validateable接口,提供了数据校验功能。通过继承该ActionSupport类,可以简化Struts 2的Action开发。
     
     
    Action 跟 Actionsupport 的区别:
    当我们在写action的时候,可以实现Action接口,也可以继承Actionsupport这个类.到底这两个有什么区别呢?
    Action接口有:
    public static final java.lang.String SUCCESS = "success";
    public static final java.lang.String NONE = "none";
    public static final java.lang.String ERROR = "error";
    public static final java.lang.String INPUT = "input";
    public static final java.lang.String LOGIN = "login";
    public abstract java.lang.String execute() throws java.lang.Exception;
    而Actionsupport这个工具类在实现了Action接口的基础上还定义了一个validate()方法,重写该方法,它会在execute()方法之前执行,如校验失败,会转入input处,必须在配置该Action时配置input属性。
     
    另外,Actionsupport还提供了一个getText(String key)方法还实现国际化,该方法从资源文件上获取国际化信息. 这样在自定义标签时可以定义一个变量为new actionsupport对象实现国际化。
     
    ActionSupport类的作用
     
    struts2不要求我们自己设计的action类继承任何的struts基类或struts接口,但是我们为了方便实现我们自己的action,大多数情况下都会继承com.opensymphony.xwork2.ActionSupport类,并重写此类里的public String execute() throws Exception方法。
    因为此类中实现了很多的实用借口,提供了很多默认方法,这些默认方法包括国际化信息的方法、默认的处理用户请求的方法等,这样可以大大的简化Acion的开发。 Struts2中通常直接使用Action来封装HTTP请求参数,
     
    因此,Action类里还应该包含与请求参数对应的属性,并且为属性提供对应的getter和setter方法。 
  • 相关阅读:
    [硬件驱动_蓝牙]蓝牙Bluez的编程实现
    二分查找
    LeetCode-35.Search Insert Position
    LeetCode-34.Find First and Last Position of Element in Sorted Array
    LeetCode-704.Binary Search
    剑指offer-最小的k个数
    树的各种遍历
    LeetCode-912.Sort an Array
    排序
    LeetCode-209.Minimum Size Subarray Sum
  • 原文地址:https://www.cnblogs.com/xuewater/p/2835156.html
Copyright © 2011-2022 走看看