Struts2從新學習之自定義攔截器(判斷用戶是不是登陸狀態)

攔截器html

  一:1:概念:Interceptor攔截器相似於咱們學習過的過濾器,是能夠再action執行先後執行的代碼。是web開發時,經常使用的技術。好比,權限控制,日誌記錄。java

    2:多個攔截器Interceptor連在一塊兒組成了Interceptor棧。攔截器是AOP面向切面編程的一種實現,具備熱插拔的效應。web

    3:Struts2攔截器,每一個攔截器類只有一個對象實例,即採用了單利模式。全部引用這個攔截器的action都共享着一攔截器類的實例。apache

 

攔截器和過濾器的區別編程

  1:攔截器和過濾器的概念很是相似session

  2:過濾器隸屬於web容器,能夠過濾一切請求(包括action,servlet,jsp,html)框架

  3:攔截器隸屬於Struts2框架,只能攔截action(沒法攔截對jsp的直接請求)jsp

  4:過濾器內部採用函數回調來實現,攔截器採用動態代理+遞歸調用來實現ide

 

自定義一個攔截器。判斷用戶是否處於登陸狀態,若是處於登陸狀態則不攔截任何操做。若是非登陸狀態,或登陸狀態靜止時間過長,致使session死亡,則攔截任何操做(除正在登陸的操做)。跳轉至登陸頁面,讓其登陸。函數

一:自定義登陸狀態判斷攔截器

 1 package com.bjsxt.shangxiaofei.interceptor;
 2 
 3 import java.util.Map;
 4 
 5 import com.bjsxt.shangxiaofei.po.User;
 6 import com.opensymphony.xwork2.ActionContext;
 7 import com.opensymphony.xwork2.ActionInvocation;
 8 import com.opensymphony.xwork2.interceptor.Interceptor;
 9 
10 public class LoginInterCeptor implements Interceptor{
11 
12     @Override
13     public void destroy() {
14         // TODO Auto-generated method stub
15         System.out.println("LoginInterCeptor.destroy(銷燬)");
16     }
17 
18     @Override
19     public void init() {
20         // TODO Auto-generated method stub
21         System.out.println("LoginInterCeptor.init(初始化)");
22     }
23 
24     @Override
25     public String intercept(ActionInvocation arg0) throws Exception {
26         //獲取session做用域
27         Map<String,Object> sessionMap=ActionContext.getContext().getSession();
28         //從session做用域中拿出登陸的用戶信息
29         User user=(User) sessionMap.get("user");
30         
31         //獲取http請求地址中的請求方法名字
32         String methodName=arg0.getProxy().getMethod();
33         //獲取http請求地址中action的名字
34         String actionName=arg0.getProxy().getActionName();
35         
36         //判斷user是否爲空,若是不爲空,說明登陸,若是爲空說明,沒有登陸,進行攔截。
37         if(user!=null){
38             //登陸,不攔截
39              System.out.println("請求時執行");
40              arg0.invoke();
41              System.out.println("響應時執行");
42              
43              return null;
44         }else{
45             //若是爲空,可是正在作登陸請求,則也不攔截
46             if(methodName.equals("loginCheck")){
47                 System.out.println("請求時執行");
48                 arg0.invoke();
49                 System.out.println("響應時執行");
50                 return null;
51             }
52             
53             sessionMap.put("backMsg", "很抱歉,請先登陸,再作操做");
54             return "login";//返回到登陸頁面
55         }
56     }
57     
58 
59 }
View Code

二:在struts.xml中配置攔截器,讓其實現做用。

 1 <?xml version="1.0" encoding="UTF-8" ?>
 2 <!DOCTYPE struts PUBLIC
 3     "-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
 4     "http://struts.apache.org/dtds/struts-2.3.dtd">
 5 
 6 <struts>
 7     <!-- 若是請求地址=actionName!methodName ,則該配置須要進行設置,不然訪問地址錯誤-->
 8     <constant name="struts.enable.DynamicMethodInvocation" value="true" />
 9     
10     <!-- 開發模式 -->
11     <constant name="struts.devMode" value="true" />
12     
13     <!-- 編碼格式過濾 -->
14     <constant name="struts.i18n.encoding" value="utf-8"></constant>
15     
16     
17     
18     <package name="default" namespace="/" extends="struts-default">
19         
20         <!-- 攔截器的配置 包括攔截器+攔截器棧 -->
21         <interceptors>
22             <interceptor name="LoginInterCeptor" class="com.bjsxt.shangxiaofei.interceptor.LoginInterCeptor"></interceptor>
23             <interceptor name="SecondInterCeptor" class="com.bjsxt.shangxiaofei.interceptor.SecondInterCeptor"></interceptor>
24             <!-- 若是想讓自定義的攔截器起做用,就必須重新配置攔截器棧,並加上原先默認的攔截器棧 -->
25             <interceptor-stack name="MyInterCeptorStack">
26                 <interceptor-ref name="defaultStack"></interceptor-ref>
27                 <interceptor-ref name="LoginInterCeptor"></interceptor-ref>
28                 <interceptor-ref name="SecondInterCeptor"></interceptor-ref>
29             </interceptor-stack>
30         </interceptors>
31         <!-- 將自定義的攔截器棧應用到項目上,項目上全部的請求都會通過該攔截器棧 -->
32         <default-interceptor-ref name="MyInterCeptorStack"></default-interceptor-ref>
33         
34     
35         <!-- actionName!methodName請求方式的配置 -->
36         <action name="userAction" class="com.bjsxt.shangxiaofei.action.UserAction" >
37               <result name="success">/page/success.jsp</result>
38         </action>
39         
40       
41     </package>
42 </struts>
View Code
相關文章
相關標籤/搜索