Struts1.x 的 Action 有两个 execute 哦,小心搞错!
by agate - Published: 2008-05-01 [9:42 下午] - Category: 程序编码

不知道各位使用 Struts1.x 的朋友们晓得不晓得,Struts1.x 的
org.apache.struts.action.Action 中有两个 execute 方法,在我们使用 eclipse 的自动完成
override 功能的时候要是不小心给弄错了你就等着迎接一个不报错的空白页面吧!让我们看看代码:

//the one  excute()
public ActionForward execute(ActionMapping mapping, ActionForm form,
ServletRequest request, ServletResponse response)throws Exception {
try {
return execute(mapping, form, (HttpServletRequest) request,
(HttpServletResponse) response);
} catch (ClassCastException e) {
return null;
}
 }
//the other excute()
public ActionForward execute(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response)
throws Exception {
return null;
}

好,公布结果!只有 override 第二个 execute 才能起作用。如果你 override 的是第一个 execute 的话,很不幸,您调用这个 action 的时候响应给你的是一个空白的页面,你也别想得到任何 exception 的提示!

在 eclipse 中我导入了 struts1.x 的 src 路径,并通过 Open Call Hierarchy 查找调用上面第一个
execute 的类时发现竟然没有调用者!换句话说当我们实现第一个 execute 的时候(没有实现第二个 execute
),是根本没用的!程序根本不会调用到我们 override 的那个 execute 只会傻傻地调用第二个 execute 的默认实现,返回一个
null

那第一个 execute 有什么用呢?我觉得他不是用来给我们重写的,看看它的内容:

 throws Exception {
try {
return execute(mapping, form, (HttpServletRequest) request,
(HttpServletResponse) response);
} catch (ClassCastException e) {
return null;
}

在我看来,其实它是为了当一个请求是一个非 http 请求的时候,作为一个前端转换器,重新包装请求和响应,然后才交给真正的,也就是我们的第二个 execute 方法来实现。这个从 src 的注释中我们也可以比较清晰的了解:

 /**
* <p>Process the specified non-HTTP request, and create the corresponding
* non-HTTP response (or forward to another web component that will create
* it), with provision for handling exceptions thrown by the business
* logic. Return an {@link ActionForward} instance describing where and
* how control should be forwarded, or <code>null</code> if the response
* has already been completed.</p>
*
* <p>The default implementation attempts to forward to the HTTP version
* of this method.</p>
*
* @param mapping The ActionMapping used to select this instance
* @param form The optional ActionForm bean for this request (if any)
* @param request The non-HTTP request we are processing
* @param response The non-HTTP response we are creating
* @return The forward to which control should be transferred, or
* <code>null</code> if the response has been completed.
* @throws Exception if the application business logic throws an
* exception.
* @since Struts 1.1
*/

顺便也说一下第二个excute的注释:

 /**
* <p>Process the specified HTTP request, and create the corresponding
* HTTP response (or forward to another web component that will create
* it), with provision for handling exceptions thrown by the business
* logic. Return an {@link ActionForward} instance describing where and
* how control should be forwarded, or <code>null</code> if the response
* has already been completed.</p>
*
* @param mapping The ActionMapping used to select this instance
* @param form The optional ActionForm bean for this request (if any)
* @param request The HTTP request we are processing
* @param response The HTTP response we are creating
* @return The forward to which control should be transferred, or
* <code>null</code> if the response has been completed.
* @throws Exception if the application business logic throws an
* exception
* @since Struts 1.1
*/

所以……当你重写这个 action 的 execute 方法时,注意咯!是重写那个参数是 http-request/response 的 execute哦!!!

这里严重鄙视一下 struts1.x 的编码态度!

Struts1.x有两个execute方法,不要重写错哦HttpServletRequest才是对的(转)的更多相关文章

  1. 12.ThreadPoolExecutor线程池原理及其execute方法

    jdk1.7.0_79  对于线程池大部分人可能会用,也知道为什么用.无非就是任务需要异步执行,再者就是线程需要统一管理起来.对于从线程池中获取线程,大部分人可能只知道,我现在需要一个线程来执行一个任 ...

  2. Java线程池中submit() 和 execute()方法的区别

    两个方法都可以向线程池提交任务, execute()方法的返回类型是void,它定义在Executor接口中, 而submit()方法可以返回持有计算结果的Future对象,它定义在ExecutorS ...

  3. Struts2 调用非execute方法

    调用非execute方法 1)如果你的Action类是继承自ActionSupport的话,确切的说是重写了execute方法,ActionSupport里的默认实现就是返回"success ...

  4. Java线程池中submit()和execute()方法有什么区别

    两个方法都可以向线程池提交任务,execute()方法的返回类型是void,它定义在Executor接口中,而submit()方法返回有计算结构的Future对象,它定义在ExecutorServic ...

  5. Java 线程池中 submit() 和 execute()方法有什么区别?

    两个方法都可以向线程池提交任务,execute()方法的返回类型是 void,它定义在 Executor 接口中. 而 submit()方法可以返回持有计算结果的 Future 对象,它定义在 Exe ...

  6. angular2系列教程(十)两种启动方法、两个路由服务、引用类型和单例模式的妙用

    今天我们要讲的是ng2的路由系统. 例子

  7. struts2 action配置时 method 省略不写 默认执行方法是父类ActionSuppot中的execute()方法

    struts2 action配置时 method 省略不写 默认执行方法是父类ActionSuppot中的execute()方法

  8. git两种合并方法 比较merge和rebase

    18:01 2015/11/18git两种合并方法 比较merge和rebase其实很简单,就是合并后每个commit提交的id记录的顺序而已注意:重要的是如果公司用了grrit,grrit不允许用m ...

  9. java 金额计算,商业计算 double不精确问题 BigDecimal,Double保留两位小数方法

    解决办法================== http://blog.javaxxz.com/?p=763 一提到Java里面的商业计算,我们都知道不能用float和double,因为他们无法 进行精 ...

随机推荐

  1. Mybatis3.x与Spring4.x整合(转)

    http://www.cnblogs.com/xdp-gacl/p/4271627.html 一.搭建开发环境 1.1.使用Maven创建Web项目 执行如下命令: mvn archetype:cre ...

  2. Object.prototype 与 Function.prototype 与 instanceof 运算符

    方法: hasOwnProperty isPrototypeOf propertyIsEnumerable hasOwnProperty 该方法用来判断一个对象中的某一个属性是否是自己提供的( 住要用 ...

  3. 项目中 poi 导出 出现html特殊符号的实体 (已解决)

    导出excel 时出现 类似这样的>  符号 , 大概是存到数据库也是这样,然后jsp解析可以解析出来,但是java不认得,需要个人写出解析方法. 废话不说,贴码: /** *转换html特殊符 ...

  4. AI第一次作业

    1.   你认为什么是人工智能?     在我看来,人工智能是是对人的意识.思维的信息过程的高度模拟,虽然它不是人的智能,但是高度模仿人的思考方式,加上它的信息高度集中,对信息的处理速度快,甚至于超过 ...

  5. git使用学习

    windows系统需要安装git,mac系统自带git git相关命令: git clone  https://git.coding.net/tenchina/wangteng_python1.git ...

  6. 课前HTML基础

    一..站点的建立 作用:用来归纳一个网站上所有的网页,素材以及他们之间的联系. 站点根文件夹的命名规则:必须是以英文或者下划线开头,后面可接数字和下划线,不可以使用中文和特殊字符. 二:创建HTML页 ...

  7. HANA学习笔记1-搭建HANA学习环境

    一 硬件环境     两台电脑,一台为服务器装跑HANA虚拟机,一台为客户端运行HANA_STUDIO     服务器:内存至少需要16G     windows server 2003 64位    ...

  8. Nginx下css的链接问题

    放在 Nginx 下的网页代码,在链接外部 css 文件时,可能出现没有链接成功的问题.需要在 nginx.conf 里的 http 下添加一行. http { include mime.types;

  9. mysql 数据库迁移

    公司的视频转码服务 使用mysql作为 任务队列, 其中mysql 是我们自己维护的 单例. 后来应业务部门建议,我们计划将现有的mysql 向dba进行迁移,以下记录一下 数据库迁移过程中的东西. ...

  10. LintCode Interleaving String

    Given three strings: s1, s2, s3, determine whether s3 is formed by the interleaving of s1 and s2. Ex ...