国产一级a片免费看高清,亚洲熟女中文字幕在线视频,黄三级高清在线播放,免费黄色视频在线看

打開APP
userphoto
未登錄

開通VIP,暢享免費(fèi)電子書等14項(xiàng)超值服

開通VIP
struts2學(xué)習(xí)筆記(9)——自定義攔截器

如果要編寫自定義攔截器,那么可以只用實(shí)現(xiàn)com.opensymphony.xwork2.interceptor.Interceptor這個(gè)接口即可。

在這個(gè)類中,需要實(shí)現(xiàn)3個(gè)方法:destroy(),init(),intercept(),核心方法是intercept(),該方法完成了主要邏輯。

在該方法中有個(gè)參數(shù)invocation,類型為ActionInvocation接口,這個(gè)接口中最重要的是invoke方法。

intercept 方法可以如此實(shí)現(xiàn):

 

 

  1. String result = invocation.invoke();   
  2.   
  3. return result;   

 

 

例如,可以這樣來(lái)實(shí)現(xiàn)自定義攔截器

 

 

  1. package cn.tshining.interceptor;   
  2.   
  3. import com.opensymphony.xwork2.ActionInvocation;   
  4. import com.opensymphony.xwork2.interceptor.Interceptor;   
  5.   
  6. public class MyInterceptor implements Interceptor {   
  7.   
  8.     public void destroy() {   
  9.         System.out.println("destory");   
  10.     }   
  11.   
  12.     public void init() {   
  13.         System.out.println("init");   
  14.     }   
  15.   
  16.     public String intercept(ActionInvocation invocation) throws Exception {   
  17.         String result = invocation.invoke();   
  18.         return result;   
  19.     }   
  20.   
  21. }   

 

 

配置攔截器

定義好攔截器后,需要在struts.xml中配置使用攔截器,配置使用攔截器有2步:

1.聲明攔截器。

在 struts.xml中package元素下增加元素

  1. <interceptors>   
  2.     <interceptor name="myinterceptor" class="cn.tshining.interceptor.MyInterceptor"></interceptor>   
  3. </interceptors>   

 


2.使用攔截器

首先需要了解的是,struts2已經(jīng)定義了一個(gè)默認(rèn)的攔截器棧,攔截器棧就是若干個(gè)攔截器或攔截器棧組成的一系列攔截器的集合。

在 struts2-core-2.1.8.jar下struts-default.xml中定義了這個(gè)默認(rèn)的攔截器棧。

在 struts中所有的action都使用了默認(rèn)的攔截器棧,若想使用自定義的攔截器棧,只用在action中配置即可,但是此時(shí)必須顯式的寫出使用默認(rèn)攔截器棧的配置信息。

 

  1. <action name="register2" class="cn.tshining.action.RegisterAction">   
  2.     <result name="success">/success.jsp</result>   
  3.     <result name="input">/register2.jsp</result>   
  4.     <interceptor-ref name="myinterceptor"></interceptor-ref>   
  5.     <interceptor-ref name="defaultStack"></interceptor-ref>   
  6. </action>   

 

 


同時(shí)可以在控制臺(tái)上看到輸出的"init",這是由于攔截器在服務(wù)器啟動(dòng)時(shí)會(huì)自動(dòng)加載,完成初始化。攔截器中定義的intercept方法就是在提交請(qǐng)求時(shí)執(zhí)行的。

用戶輸入的數(shù)據(jù)會(huì)自動(dòng)賦值到action中,實(shí)際上是底層攔截器起的作用。

在 struts-default.xml中可以看到名為params的攔截器,這個(gè)攔截器的作用就是將用戶輸入的數(shù)據(jù)注入到Action中的字段中。

使用攔截器參數(shù)

在定義攔截器,或者在使用攔截器時(shí)可以給攔截器傳遞參數(shù)。

首先在 MyInterceptor類中聲明一下字段

public int myparam;

修改 intercept方法:

 

  1. public String intercept(ActionInvocation invocation) throws Exception {   
  2.     String result = invocation.invoke();   
  3.     System.out.println(myparam);   
  4.     return result;   
  5. }   

 

 


修改struts.xml中攔截器的定義:

 

  1. <interceptors>   
  2.     <interceptor name="myinterceptor" class="cn.tshining.interceptor.MyInterceptor">    
  3.         <param name="myparam">1</param>    
  4.     </interceptor>    
  5. </interceptors>   

 

 


然后運(yùn)行該程序,在register2.jsp中輸入正確數(shù)據(jù)提交后就會(huì)在控制臺(tái)中輸出1.

也可以在攔截器調(diào)用時(shí)再傳入?yún)?shù):

 

  1. <action name="register2" class="cn.tshining.action.RegisterAction">   
  2.     <result name="success">/success.jsp</result>   
  3.     <result name="input">/register2.jsp</result>   
  4.     <interceptor-ref name="myinterceptor">   
  5.         <param name="myparam">2</param>   
  6.     </interceptor-ref>   
  7.     <interceptor-ref name="defaultStack"></interceptor-ref>   
  8. </action>   

 


此時(shí)重啟服務(wù)器,在register2.jsp中輸入正確的數(shù)據(jù)之后,可以看到控制臺(tái)中輸出2.

注意此時(shí)并沒(méi)有刪除之前攔截器定義時(shí)的參數(shù),由此可見(jiàn),在攔截器調(diào)用時(shí)的參數(shù)比攔截器定義時(shí)優(yōu)先級(jí)高。

上面的攔截器調(diào)用中使用到了默認(rèn)攔截器棧,當(dāng)然自己也可以自定義默認(rèn)攔截器棧。

  1. <interceptors>   
  2.     <interceptor name="myinterceptor" class="cn.tshining.interceptor.MyInterceptor">   
  3.         <param name="myparam">1</param>   
  4.     </interceptor>   
  5.     <interceptor-stack name="myDefaultStack">   
  6.         <interceptor-ref name="myinterceptor"></interceptor-ref>   
  7.         <interceptor-ref name="defaultStack"></interceptor-ref>   
  8.     </interceptor-stack>   
  9. </interceptors>   
  10. <default-interceptor-ref name="myDefaultStack"></default-interceptor-ref>  

 

 

這樣,這個(gè)自定義的默認(rèn)攔截器棧中包含了自定義的攔截器和struts2定義的默認(rèn)攔截器棧。

在攔截器棧的定義中,既可以引用攔截器,也可以引用攔截器棧。

在 action中沒(méi)定義攔截器時(shí),會(huì)使用默認(rèn)攔截器。

當(dāng)然 struts2提供了一個(gè)簡(jiǎn)便的創(chuàng)建自定義攔截器的方法。

只需要繼承AbstractInterceptor類,該類空實(shí)現(xiàn)了Interceptor接口的init和destroy方法,因此只用實(shí)現(xiàn) intercept方法即可。

在這里我們override AbstractInterceptor的init方法。

 

 

  1. package cn.tshining.interceptor;   
  2.   
  3. import com.opensymphony.xwork2.ActionInvocation;   
  4. import com.opensymphony.xwork2.interceptor.AbstractInterceptor;   
  5.   
  6. public class MyInterceptor2 extends AbstractInterceptor {   
  7.   
  8.     @Override   
  9.     public String intercept(ActionInvocation invocation) throws Exception {   
  10.         String result = invocation.invoke();   
  11.         return result;   
  12.     }   
  13.   
  14.     @Override   
  15.     public void init() {   
  16.         System.out.println("init2");   
  17.     }   
  18. }   

 

 

在<interceptor-ref name="myinterceptor"></interceptor-ref>下面增加以下內(nèi)容:

 

  1. <interceptor-ref name="myinterceptor2"></interceptor-ref>   

 

 

這樣就將myinterceptor2增加到了默認(rèn)攔截器棧中,查看控制臺(tái)輸出會(huì)看到:

init

init2

銷毀時(shí)會(huì)看到:

destory2

destory

這是由于先配置的攔截器先執(zhí)行,結(jié)束時(shí)后配置的攔截器先執(zhí)行

在這里攔截器的執(zhí)行順序?yàn)椋?

myintercept——myintercept2——action——myintercept2——myintercept

需要說(shuō)明的是一定要在action中引用攔截器才會(huì)使用攔截器,攔截器攔截的是Action中的execute或自定義的邏輯處理方法。

當(dāng)然也可以讓攔截器攔截指定的方法,那么我們需要繼承MethodFilterinterceptor類

 

  1. package cn.tshining.interceptor;   
  2.   
  3. import com.opensymphony.xwork2.ActionInvocation;   
  4. import com.opensymphony.xwork2.interceptor.MethodFilterInterceptor;   
  5.   
  6. public class MyInterceptor3 extends MethodFilterInterceptor {   
  7.   
  8.     @Override   
  9.     protected String doIntercept(ActionInvocation invocation) throws Exception {   
  10.         String result = invocation.invoke();   
  11.         System.out.println("doing");   
  12.         return result;   
  13.     }   
  14. }   

 

 


在struts.xml中聲明攔截器

 

  1. <interceptor name="myinterceptor3" class="cn.tshining.interceptor.MyInterceptor3"></interceptor>   

 

 

然后在 Action中引用此攔截器。

首先查看MethodFilterinterceptor,它接受兩個(gè)參數(shù),

excludeMethods:排除的方法,多個(gè)方法間用逗號(hào)隔開

includeMethods:包含的方法,多個(gè)方法間用逗號(hào)隔開

在 struts.xml中如下配置

 

  1. <interceptor-ref name="myinterceptor3">   
  2.     <param name="excludeMethods">test,execute</param>   
  3.     <param name="includeMethods">test</param>   
  4. </interceptor-ref>   

 

 

那么該攔截其將會(huì)攔截test方法,因?yàn)閕nclude比exclude優(yōu)先級(jí)高

在 com.opensymphony.xwork2.interceptor包里面還有一個(gè)重要的接口PreResultListener,查看文檔。

  * PreResultListeners may be registered with an ActionInvocation to get a callback after the Action has been executed but before the Result is executed.

注釋:

    PreResultListeners 可以注冊(cè)到ActionInvocation,當(dāng)Action執(zhí)行完畢但是在返回結(jié)果之前調(diào)用。
也就是在action的execute執(zhí)行結(jié)束之后,在渲染頁(yè)面前調(diào)用。

首先實(shí)現(xiàn)自己的PreResultListeners類

 

  1. package cn.tshining.interceptor;   
  2.   
  3. import com.opensymphony.xwork2.ActionInvocation;   
  4. import com.opensymphony.xwork2.interceptor.PreResultListener;   
  5.   
  6. public class MyListener implements PreResultListener {   
  7.   
  8.     public void beforeResult(ActionInvocation invocation, String resultCode) {   
  9.         System.out.println(resultCode);   
  10.     }   
  11. }   

 


修改自己的攔截器MyInterceptor3中的 doIntercept方法。

 

  1. protected String doIntercept(ActionInvocation invocation) throws Exception {   
  2.     invocation.addPreResultListener(new MyListener());   
  3.     String result = invocation.invoke();   
  4.     System.out.println("doing");   
  5.     return result;   
  6. }   

 


注意PreResultListeners必須向 ActionInvocation實(shí)例去注冊(cè),而且注冊(cè)要在調(diào)用ActionInvocation實(shí)例的invoke方法之前。

當(dāng)在 register2.jsp中輸入正確的數(shù)據(jù)時(shí),在控制臺(tái)上可以看到輸出的success信息。

該執(zhí)行過(guò)程為:

攔截器 ——validate——execute——beforeResult——結(jié)束

攔截器的應(yīng)用

用攔截器進(jìn)行權(quán)限校驗(yàn)

首先分析一下這個(gè)示例的工作流程:

首先用戶從login2.jsp登陸,只有username輸入hello,password輸入world,然后跳轉(zhuǎn)到register2.jsp進(jìn)行注冊(cè)操作。

如果用戶直接訪問(wèn)register2.jsp,提交后將直接跳轉(zhuǎn)回login2.jsp。

1.首先我們修改struts2.xml,讓用戶在login2.jsp中輸入正確頁(yè)面之后,將頁(yè)面轉(zhuǎn)到register2.jsp。

 

  1. <action name="login" class="cn.tshining.action.LoginAction">   
  2.     <result name="success">/register2.jsp</result>   
  3.     <result name="input">/login2.jsp</result>   
  4.     <result name="failer">/login2.jsp</result>   
  5. </action>   

 


2.修改LoginAction類中execute方法

 

  1. public String execute() throws Exception {   
  2.   
  3.     if ("hello".equals(this.getUsername())   
  4.             && "world".equals(this.getPassword())) {   
  5.         Map<String,Object> map = ActionContext.getContext().getSession();   
  6.         map.put("user""login");   
  7.         return "success";   
  8.     } else {   
  9.         this.addFieldError("username""username or password error");   
  10.         return "failer";   
  11.     }   
  12.   
  13. }   

 


在該方法中,若用戶輸入正確信息后,會(huì)獲得session對(duì)象,并把 user和login存到session中。

struts2 的Action中獲得session對(duì)象的方法是

Map map = ActionContext.getContext().getSession();

struts 將session從servlet容器中抽離出來(lái)了,形成了一個(gè)map,更利于單元測(cè)試
3.新建一個(gè)攔截器。

 

 

  1. package cn.tshining.interceptor;   
  2.   
  3. import java.util.Map;   
  4.   
  5. import com.opensymphony.xwork2.Action;   
  6. import com.opensymphony.xwork2.ActionInvocation;   
  7. import com.opensymphony.xwork2.interceptor.AbstractInterceptor;   
  8.   
  9. public class AuthInterceptor extends AbstractInterceptor {   
  10.   
  11.     @Override   
  12.     public String intercept(ActionInvocation invocation) throws Exception {   
  13.         Map<String,Object> map = invocation.getInvocationContext().getSession();   
  14.         if(map.get("user") == null){   
  15.             return Action.LOGIN;   
  16.         }else{   
  17.             return invocation.invoke();   
  18.         }   
  19.     }   
  20. }   

 

 

在這個(gè)攔截器中,獲得session對(duì)象的方法是

 

  1. Map<String,Object> map = invocation.getInvocationContext().getSession();   

 

 

因此,當(dāng)session中不存在user的值為login時(shí),攔截器將返回Action.LOGIN,在struts.xml中返回結(jié)果為login將跳轉(zhuǎn)到 login2.jsp。

這樣便可以實(shí)現(xiàn)一個(gè)簡(jiǎn)單的用戶校驗(yàn)了。

4.配置action

 

  1. <action name="register2" class="cn.tshining.action.RegisterAction">   
  2.     <result name="success">/success.jsp</result>   
  3.     <result name="input">/register2.jsp</result>   
  4.     <result name="login">/login2.jsp</result>   
  5.     <interceptor-ref name="logininterceptor"></interceptor-ref>   
  6.     <interceptor-ref name="defaultStack"></interceptor-ref>   
  7. </action>   

 

 

這樣就可以正常工作了。

當(dāng)然也可以不在Action中定義login的結(jié)果跳轉(zhuǎn),而直接在全局結(jié)果中配置。將下面代碼放到Action配置前即可正常工作。

 

  1. <global-results>   
  2.     <result name="login">/login2.jsp</result>   
  3. </global-results>   

 


這樣就定義了一個(gè)全局結(jié)果頁(yè)面。

直接進(jìn)入到register2.jsp,點(diǎn)提交后,會(huì)發(fā)現(xiàn)地址欄跳到了http://localhost:8888/struts2 /register.action

結(jié)果重定向

前面在結(jié)果轉(zhuǎn)發(fā)時(shí)都是用到了請(qǐng)求轉(zhuǎn)發(fā),實(shí)際上struts2定義了很多結(jié)果類型,我們可以使用重定向試試。

打開 struts2-core-2.1.8.jar,查看里面的struts-default.xml,其中定義了很多結(jié)果類型。

其中的 redirect代表的就是重定向。

然后將上面Action配置中作如下修改:

 

  1. <result name="login" type="redirect">/login2.jsp</result>   

 

 

然后重啟服務(wù)器,直接進(jìn)入到register2.jsp,點(diǎn)提交后,會(huì)發(fā)現(xiàn)地址欄跳到了http://localhost:8888/struts2 /login2.jsp

這就是重定向。在struts2中默認(rèn)是dispatcher。

下次將學(xué)習(xí)struts2的文件上傳與下載。

本站僅提供存儲(chǔ)服務(wù),所有內(nèi)容均由用戶發(fā)布,如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請(qǐng)點(diǎn)擊舉報(bào)。
打開APP,閱讀全文并永久保存 查看更多類似文章
猜你喜歡
類似文章
Struts2攔截器總結(jié)
Struts2 攔截器實(shí)現(xiàn)權(quán)限控制
Struts2的攔截器總結(jié)
Struts2攔截器和監(jiān)聽器
Struts2 攔截器詳細(xì)配置過(guò)程
2.5.3使用 paramsPrepareParamsStack 攔截器棧后的運(yùn)行流程
更多類似文章 >>
生活服務(wù)
分享 收藏 導(dǎo)長(zhǎng)圖 關(guān)注 下載文章
綁定賬號(hào)成功
后續(xù)可登錄賬號(hào)暢享VIP特權(quán)!
如果VIP功能使用有故障,
可點(diǎn)擊這里聯(lián)系客服!

聯(lián)系客服