日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

37.使用PreResultListener实现回调

發布時間:2024/4/13 编程问答 29 豆豆
生活随笔 收集整理的這篇文章主要介紹了 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對象會在返回結果前執行,請注意結合攔截器執行的順序來看。此實例目前作為了解。

總結

以上是生活随笔為你收集整理的37.使用PreResultListener实现回调的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。