5.6 PreResultListener
5.6.1 什么是PreResultListener
在实际开发当中,常常需要在Action执行完毕后,而Result还没有开始执行的时候,做一些功能处理,比如异常处理,那么该怎么做呢?
这就需要使用PreResultListener了。看名字就知道,这是一个“Listener”,也就是一个监听器,用来执行事件监听的。那么它监听的到底是什么事件呢?
PreResultListener监听的事件就是Action执行完毕,马上要开始Result的处理了,这么一个事件。监听到这个事件后,由谁来进行处理呢?
跟Java里面其他的事件机制一样,需要由我们去实现一个实现PreResultListener的类,在这个类里面去实现相应的事件回调方法,就是在这些方法里面,去实现事件的处理。
另外一个问题,当我们自己写了一个实现PreResultListener的类,可是Struts2怎么知道呢?跟Java里面其他的事件机制一样,需要注册监听器,而且要在事件触发之前注册才有效。好了,一起来看看示例。
5.6.2 PreResultListener实现示例
(1)先要写一个类来实现PreResultListener,示例如下:
java代码:
- package cn.javass.action.action;
-
- import com.opensymphony.xwork2.ActionInvocation;
- import com.opensymphony.xwork2.interceptor.PreResultListener;
-
- public class MyPreResult implements PreResultListener{
- public void beforeResult(ActionInvocation actionInvocation, String result) {
- System.out.println("现在处理Result执行前的功能,result="+result);
- }
- }
这个类中出现了ActionInvocation对象,这个对象封装了Action运行所需要的东西,包括数据、代理对象等,甚至可以通过它来执行Action。这里只是简单的示例一下,更多的功能,请参见相应的API文档。
(2)然后在Action的execute方法里面,注册这个监听器对象,示例如下:
java代码:
- package cn.javass.action.action;
- import com.opensymphony.xwork2.ActionContext;
- import com.opensymphony.xwork2.ActionSupport;
- import com.opensymphony.xwork2.interceptor.PreResultListener;
- public class HelloWorldAction extends ActionSupport {
- private String account;
- private String password;
- private String submitFlag;
-
- public String execute() throws Exception {
- this.businessExecute();
- PreResultListener pr = new MyPreResult(); ActionContext.getContext().getActionInvocation().addPreResultListener(pr);
- return this.SUCCESS;
- }
-
-
-
- public void businessExecute(){
- System.out.println("用户输入的参数为==="+"account="+account+",password="+password+",submitFlag="+submitFlag);
- }
-
- }
(3)struts.xml没有什么变化,就是最简单的配置,如下:
java代码:
- <?xml version="1.0" encoding="UTF-8" ?>
- <!DOCTYPE struts PUBLIC
- "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
- "http://struts.apache.org/dtds/struts-2.0.dtd">
-
- <struts>
- <constant name="struts.devMode" value="true" />
- <constant name="struts.locale" value="zh_CN"/>
- <constant name="struts.i18n.encoding" value="gb2312"/>
-
- <package name="helloworld" extends="struts-default">
- <action name="helloworldAction" class="cn.javass.action.action.HelloWorldAction">
- <result>/s2impl/welcome.jsp</result>
- </action>
- </package>
- </struts>
(4)为了看出PreResult确实是在Result之前运行的,可以在welcome.jsp里面简单的输出一句话,如下所示:
java代码:
- <%@ page language="java" contentType="text/html; charset=gb2312"
- pageEncoding="gb2312"%>
- <html>
- <head>
- <meta http-equiv="Content-Type" content="text/html; charset=gb2312">
- <title>Insert title here</title>
- </head>
- <body>
- <%
- System.out.println("现在输出Result页面");
- %>
- </body>
- </html>
(5)运行一下,看看效果,界面上没有什么特殊之处,看看后台,输出如下:
java代码:
- 用户输入的参数为===account=null,password=null,submitFlag=null
- 现在处理Result执行前的功能,result=success
- 现在输出Result页面
表示当Action运行后,这个事件监听器监听到了相应的事件,并执行了相应的功能处理,处理完后才运行了Result的功能。
私塾在线网站原创《研磨struts2》系列
转自请注明出处:【http://sishuok.com/forum/blogPost/list/0/4055.html】
欢迎访问http://sishuok.com获取更多内容
分享到:
相关推荐
研磨Struts2
《研磨Struts2》原书配套源代码,配合Struts2原书使用,快速学习Struts2技术。
由于文件较大,我把这个文档切割为2部分,这是第一部分,请下载完第一部分后一定要下载第二部分,否则不能阅读。
研磨struts2博文,详细讲解了struts2运行流程,值得大家阅读
《研磨Struts2》原书配套PPT,结合《研磨Struts2》配套使用,快速学习Struts2框架。
研磨struts2_系列文章.pdf
研磨Struts2 高清完整版,请和第一部分一起下载啊
《研磨Struts 2》PDF版本下载
研磨Struts2_12859679_高清完整版
研磨Struts2-高清-完整目录-2011年10月,分享给所有需要的人
研磨struts,仅是简版,我也是从网上下来的。大家凑合看吧。
第5章 单例模式(Singleton) 第6章 工厂方法模式(Factory Method) 第7章 抽象工厂模式(Abstract Factory) 第8章 生成器模式(Builder) 第9章 原型模式(Prototype) 第10章 中介者模式(Mediator) 第11...
第5章 单例模式(Singleton) 第6章 工厂方法模式(Factory Method) 第7章 抽象工厂模式(Abstract Factory) 第8章 生成器模式(Builder) 第9章 原型模式(Prototype) 第10章 中介者模式(Mediator) 第11...
Java工程师三大框架面试题.pdf
第5章 单例模式(Singleton) 第6章 工厂方法模式(Factory Method) 第7章 抽象工厂模式(Abstract Factory) 第8章 生成器模式(Builder) 第9章 原型模式(Prototype) 第10章 中介者模式(Mediator) 第11...
第5章 单例模式(Singleton) 第6章 工厂方法模式(Factory Method) 第7章 抽象工厂模式(Abstract Factory) 第8章 生成器模式(Builder) 第9章 原型模式(Prototype) 第10章 中介者模式(Mediator) ...