zoukankan      html  css  js  c++  java
  • 37.使用PreResultListener实现回调

    转自:https://wenku.baidu.com/view/84fa86ae360cba1aa911da02.html

    在进行本实例前请前复习:五.2自定义拦截器。因为PreResultListener对象一般是绑定在拦截器上使用。

    下面我们新建struts2PreResultListener项目进行测试。

    步骤一,建立类,实现PreResultListener接口,主要代码如下:

    package com.asm;

    import com.opensymphony.xwork2.ActionInvocation;

    import com.opensymphony.xwork2.interceptor.PreResultListener;

    public class MyPreResultListener implements PreResultListener {

           public void beforeResult(ActionInvocation invocation, String res) {

                  // System.out.println(invocation.getAction());

                  // System.out.println(invocation.getResultCode());

                  /**回调Action中的方法:

                   * LoginAction lg = (LoginAction) invocation.getAction(); try {

                   * lg.execute(); } catch (Exception e) { e.printStackTrace(); }

                   */

                  System.out.println("检验到PreResultListener被执行");

           }

    }                                                       8888888

    步骤二,copy前面在自定义拦截器中用到的三个拦截器,并绑定MyPreResultListener对象,首先是在MyInterceptor类中,我们只需要修改intercept方法即可,代码如下:

    public String intercept(ActionInvocation invocation) throws Exception {

                  invocation.addPreResultListener(new MyPreResultListener());

                  System.out.println("开始拦截");

                  String result = invocation.invoke();

                  System.out.println("结束拦截");

                  return result;

    }

    随后在MyMethodFilterInterceptor类中作类似修改。为了区别,我们在MyAbstractInterceptor类中不绑定MyPreResultListener对象。

    步骤三,编写struts.xml文件,主要配置内容如下:
    <struts>

           <package name="interceptor" extends="struts-default">

                  <interceptors>

                         <interceptor name="myIpt" class="com.asm.MyInterceptor">

                         </interceptor>

                         <interceptor name="myAbs"

                                class="com.asm.MyAbstractInterceptor">

                         </interceptor>

                         <interceptor name="myMet"

                                class="com.asm.MyMethodFilterInterceptor">

                         </interceptor>

                  </interceptors>

     

                  <action name="login" class="com.asm.LoginAction">

                         <interceptor-ref name="myIpt"></interceptor-ref>

                         <interceptor-ref name="myAbs"></interceptor-ref>

                         <interceptor-ref name="myMet"></interceptor-ref>

                         <result name="success">/success.jsp</result>

                  </action>                         

           </package>

    </struts>

    步骤四,编写相应的jsp页面,发布测试。

    说明:此实例的只是简要地演示了PreResultListener的使用,所以相对简单。对于其它相关操作,我们可以从MyPreResultListener类注释掉的内容中找到一此端倪。强调:从执行结果来看,PreResultListener对象会在返回结果前执行,请注意结合拦截器执行的顺序来看。此实例目前作为了解。

  • 相关阅读:
    fileupload的乱码解决
    关于WEB-INF文件夹中的内容
    复习,关于server.xml的一点理解
    使用eclipse kepler,结果getServletContext可用了
    拷贝内容到eclipse中导致JSP的auto-completion不工作
    eclipse新建tomcat server但是总是报404的解决方法
    flex 自定义事件
    flex TweenLite
    flex 坐标系
    flex DataGroup
  • 原文地址:https://www.cnblogs.com/sharpest/p/5587647.html
Copyright © 2011-2022 走看看