《研磨struts2》第五章 Result 之 5.6 PreResultListener
来源:互联网 发布:超图软件千股千评 编辑:程序博客网 时间:2024/05/16 19:03
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,示例如下:
- 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方法里面,注册这个监听器对象,示例如下:
- 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);
- }
- //属性对应的getter/setter方法,省略了
- }
(3)struts.xml没有什么变化,就是最简单的配置,如下:
- <?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里面简单的输出一句话,如下所示:
- <%@ 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)运行一下,看看效果,界面上没有什么特殊之处,看看后台,输出如下:
- 用户输入的参数为===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》第五章 Result 之 5.6 PreResultListener
- 《研磨struts2》第五章 Result 之 5.1 Result基础
- 《研磨struts2》第五章 Result 之 5.3 全局Result
- 《研磨struts2》第五章 Result 之 5.7 自定义Result
- 《研磨struts2》第五章 Result 之 5.4 使用通配符
- 《研磨struts2》第五章 Result 之 5.2 预定义的Result
- 《研磨struts2》第五章 Result 之 5.5 Struts2的异常映射
- Struts2.x 之 使用PreResultListener
- 《研磨struts2》 第二章 Struts2的HelloWorld 之 概述
- 《研磨struts2》第四章 Action 之 4.1 Action基础
- 《研磨struts2》第四章 Action 之 4.2 Action的实现
- 《研磨struts2》第四章 Action 之 4.3 Action的数据
- 《研磨struts2》第四章 Action 之 4.4 Action的配置
- 《研磨struts2》第九章 国际化 之 9.1 概述
- Struts2拦截器-PreResultListener
- Struts2拦截器-PreResultListener
- Struts2使用PreResultListener
- 《研磨struts2》第三章 Struts2的架构和运行流程 之 3.1 Struts2的系统架构
- 《研磨struts2》第五章 Result 之 5.5 Struts2的异常映射
- JAVA中的几种对象解释
- te、KB、MB、GB、TB、PB、EB、ZB、YB、NB你知多少
- 对UILabel配置手势识别的方法
- 关于彩虹表
- 《研磨struts2》第五章 Result 之 5.6 PreResultListener
- HDOJ 1203 I NEED A OFFER! (01背包)
- 《研磨struts2》第五章 Result 之 5.7 自定义Result
- hdu 1217 Arbitrage
- oracle数据库性能调优
- 《研磨struts2》 第六章 拦截器 之 6.1 拦截器基础
- 用supermemo背单词达到7000词条
- OPENCV中MAT与IPLIMAGE和CVMAT类型之间的相互转换
- 《研磨struts2》 第六章 拦截器 之 6.2 预定义的拦截器