一区二区三区在线-一区二区三区亚洲视频-一区二区三区亚洲-一区二区三区午夜-一区二区三区四区在线视频-一区二区三区四区在线免费观看

服務器之家:專注于服務器技術及軟件下載分享
分類導航

PHP教程|ASP.NET教程|Java教程|ASP教程|編程技術|正則表達式|C/C++|IOS|C#|Swift|Android|VB|R語言|JavaScript|易語言|vb.net|

服務器之家 - 編程語言 - Java教程 - Struts2攔截器 關于解決登錄的問題

Struts2攔截器 關于解決登錄的問題

2021-01-16 11:05南城琉璃 Java教程

下面小編就為大家帶來一篇Struts2攔截器 關于解決登錄的問題。小編覺得挺不錯的,現在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧

Struts2攔截器 關于解決登錄的問題

攔截器的工作原理如圖 攔截器是由每一個action請求(request)都包裝在一系列的攔截器的內部,通過redirectAction再一次發送請求。

攔截器可以在Action執行直線做相似的操作也可以在Action執行直后做回收操作。

我們可以讓每一個Action既可以將操作轉交給下面的攔截器,Action也可以直接退出操作返回客戶既定的畫面。

接下來我們該如何定義一個攔截器:

自定義一個攔截器如下:

1、實現Interceptor接口或者繼承AbstractInterceptor抽象類。

2、創建一個Struts.xml文件進行定義攔截器。

3、在需要使用的Action中引用上述定義的攔截器,為了方便也可將攔截器定義為默認的攔截器(<default-interceptor-ref name="myStack"/>),

這樣在不加特殊聲明的情況下所有的Action都被這個攔截器攔截<param name="excludeMethods">loginView,login</param>。

①Interceptor接口聲明三個方法:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
public class LoginInterceptor implements Interceptor {
 
 private Map<String,Object> session = null;
 public void destroy() { }
 public void init() { }
 public String intercept(ActionInvocation actionInvocation) throws Exception {
 8     Object myAction = actionInvocation.getAction();
  if(myAction instanceof UserAction){
   System.out.println("你訪問的Action是UserAction,不要校驗Session,否則死循環");
   //放行
   return actionInvocation.invoke();
  }else{
   System.out.println("你訪問的Action是:"+myAction);
  }
 
  session = ActionContext.getContext().getSession();
  Object user = session.get("user");
  if (user!=null){
   return actionInvocation.invoke();
  }else{
   return "login";
  }
 
}
 
注:該方法可以不加:<param name="excludeMethods">loginView,login</param>

②讓它繼承 MethodFilterInterceptor:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
public class LoginInterceptor extends MethodFilterInterceptor {
 private Map<String,Object> session = null;
 protected String doIntercept(ActionInvocation actionInvocation) throws Exception {
  /*
  Object myAction = actionInvocation.getAction();
  if(myAction instanceof UserAction){
   System.out.println("你訪問的Action是UserAction,不要校驗Session,否則死循環");
   //放行
   return actionInvocation.invoke();
  }else{
   System.out.println("你訪問的Action是:"+myAction);
  }
  */
  session = ActionContext.getContext().getSession();
  Object user = session.get("user");
  if (user!=null){
   return actionInvocation.invoke();
  }else{
   return "login";
  }
 }
}

③UserAction繼承ActionSupport 實現 ModelDriven<User>和SessionAware:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
public class UserAction extends ActionSupport implements ModelDriven<User>,SessionAware{
 
 private Map<String,Object> session = null;
 private User user = null;
   //驅動模型
 public User getModel() {
  this.user = new User();
  return this.user;
 }
 
 public void setSession(Map<String, Object> map) {
  this.session = map;
 }
 
 public String loginView(){
  return "loginViewSuccess";
 }
 
 public String login(){
  if ("admin".equals(user.getUserName())&&"123456".equals(user.getUserPassword())){
   session.put("user",user);
   return this.SUCCESS;
  }else{
   return this.ERROR;
  }
 
 }
}

Struts.xml文件中:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
<struts>
 <package name="myPackage" extends="struts-default">
 
  <interceptors>
 
   <interceptor name="loginInterceptor" class="com.nf.action.LoginInterceptor"></interceptor>
   <interceptor-stack name="myStack">
    <interceptor-ref name="loginInterceptor">
     <!--excludeMethods需要生效的話,自定義的攔截器,不能使用實現Interceptor接口,而是extends MethodFilterInterceptor-->
     <param name="excludeMethods">loginView,login</param><!--不用此行時 我們可以配合①使用攔截器-->
    </interceptor-ref>
    <interceptor-ref name="defaultStack"></interceptor-ref>
   </interceptor-stack>
  </interceptors>
  <!--配置一個默認攔截器,也就是所有的Action都必須使用-->
  <default-interceptor-ref name="myStack"/>
  <global-results>
   <result name="login" type="redirectAction">userAction_loginView</result>
  </global-results>
  <!--不寫method,默認就是execute-->
  <action name="indexAction" class="com.nf.action.IndexAction" method="execute">
   <result name="success">/WEB-INF/jsp/index.jsp</result>
   <!--
   <interceptor-ref name="myStack"></interceptor-ref>
   -->
   <!--注釋這里也可以放該代碼 只不過每一個action都要放比較麻煩
   <interceptor-ref name="loginInterceptor"></interceptor-ref>
   <interceptor-ref name="defaultStack"></interceptor-ref>
   -->
  </action>
 
  <action name="otherFunctionAction" class="com.nf.action.OtherFunctionAction">
   <!--不寫name,默認就是success-->
   <result>/WEB-INF/jsp/otherFunction.jsp</result>
  </action>
  
  <action name="userAction_*" class="com.nf.action.UserAction" method="{1}">
   <result name="loginViewSuccess">/WEB-INF/jsp/loginView.jsp</result>
   <result name="error">/WEB-INF/jsp/error.jsp</result>
   <result name="success" type="redirectAction">indexAction</result>
   <allowed-methods>login,loginView</allowed-methods>
  </action>
 </package>
</struts>

其中,<param name="excludeMethods">loginView,login</param> 配置的過濾方法,意思是攔截器對其中的方法不起作用。在我這里,loginView是跳轉到登錄頁面的方法。

login 是驗證用戶名和密碼的方法,在其中會將通過驗證的用戶名放入session中。

總結:

1.在struts2 中,所有的攔截器都會繼承 Interceptor 這個接口。

2.如果我們沒有添加攔截器,struts2 會為我們添加默認攔截器。當然我們要是指定了攔截器,我們自己的攔截器就會取代默認的攔截器,

那么我們就不能享受默認攔截器提供的一些功能。所以,一般我會把默認攔截器也加上。

例如,在以上配置項中,action 里面再加上<interceptor-ref name="defaultStack"></interceptor-ref>

以上這篇Struts2攔截器 關于解決登錄的問題就是小編分享給大家的全部內容了,希望能給大家一個參考,也希望大家多多支持服務器之家。

原文鏈接:http://www.cnblogs.com/wangpengpeng/archive/2017/10/10/7647599.html

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 免费免费啪视频在线观播放 | 纲手被强喷水羞羞漫画 | 精品国产综合区久久久久久 | 日本在线亚州精品视频在线 | voyeur多毛厕所 | 国产91在线精品 | 天天做天天爱天天爽综合网 | 五月色综合婷婷综合俺来也 | 短篇同学新婚h系列小说 | 四虎影院精品在线观看 | 1024在线视频精品免费 | 欧美精品一线二线大片 | 91嫩草国产在线观看免费 | 成人aaaa | 亚洲精品m在线观看 | 四虎国产一区 | 久久视频在线视频 | 亚洲欧洲综合 | 色橹橹| tobu8中国在线播放免费 | 精品久久久久久亚洲 | 俄罗斯三级在线观看级 | 亚洲熟区 | 国产一区二区三区欧美精品 | 国产精品福利一区二区亚瑟 | 国产第一福利视频导航在线 | 97蝌蚪自拍自窝 | 成人午夜影院在线观看 | 午夜dj影院在线观看完整版 | 女人把扒开给男人爽 | 非洲黑人bbwbbwbbw | 亚洲国产精品成人午夜在线观看 | 欧美一级裸片 | 亚洲精品国产精麻豆久久99 | 粉嫩极品国产在线观看免费 | 国产专区视频在线观看 | 免费国产高清精品一区在线 | 调教小龙女 | 亚洲 欧美 制服 校园 动漫 | 日本高清va不卡视频在线观看 | 麻豆视频网 |