最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

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

 更新時間:2017年10月11日 09:28:46   作者:南城琉璃  
下面小編就為大家?guī)硪黄猄truts2攔截器 關于解決登錄的問題。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧

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

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

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

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

自定義一個攔截器如下:

1、實現(xiàn)Interceptor接口或者繼承AbstractInterceptor抽象類。

2、創(chuàng)建一個Struts.xml文件進行定義攔截器。

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

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

①Interceptor接口聲明三個方法:

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,否則死循環(huán)");
   //放行
   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:

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,否則死循環(huán)");
   //放行
   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 實現(xiàn) ModelDriven<User>和SessionAware:

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文件中:

<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需要生效的話,自定義的攔截器,不能使用實現(xiàn)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攔截器 關于解決登錄的問題就是小編分享給大家的全部內容了,希望能給大家一個參考,也希望大家多多支持腳本之家。

相關文章

  • bug解決Failed_to_execute_goal_org.springframework

    bug解決Failed_to_execute_goal_org.springframework

    這篇文章主要為大家介紹了bug解決Failed_to_execute_goal_org.springframework,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪
    2023-09-09
  • SpringBoot之@Value獲取application.properties配置無效的解決

    SpringBoot之@Value獲取application.properties配置無效的解決

    這篇文章主要介紹了SpringBoot之@Value獲取application.properties配置無效的解決方案,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2023-03-03
  • 淺談Java中的Queue家族

    淺談Java中的Queue家族

    Java中Collection集合有三大家族List,Set和Queue。當然Map也算是一種集合類,但Map并不繼承Collection接口。List,Set在我們的工作中會經常使用,通常用來存儲結果數(shù)據(jù),而Queue由于它的特殊性,通常用在生產者消費者模式中。今天這篇文章將帶大家進入Queue家族。
    2021-06-06
  • java實現(xiàn)圖書館管理系統(tǒng)

    java實現(xiàn)圖書館管理系統(tǒng)

    這篇文章主要為大家詳細介紹了java實現(xiàn)圖書館管理系統(tǒng),文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2019-10-10
  • Mybatis框架之代理模式(Proxy Pattern)的實現(xiàn)

    Mybatis框架之代理模式(Proxy Pattern)的實現(xiàn)

    本文主要介紹了MyBatis框架中使用代理模式ProxyPattern的原理和實現(xiàn),文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2024-11-11
  • spring項目中切面及AOP的使用方法

    spring項目中切面及AOP的使用方法

    我們知道,spring兩大核心,IOC(控制反轉)和AOP(切面),那為什么要使用AOP,AOP是什么呢?帶著這些問題通過本文學習下吧
    2021-06-06
  • Java圖像處理之RGB調色面板

    Java圖像處理之RGB調色面板

    這篇文章主要為大家詳細介紹了Java圖像處理之RGB調色面板,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2022-06-06
  • Netty分布式ByteBuf使用SocketChannel讀取數(shù)據(jù)過程剖析

    Netty分布式ByteBuf使用SocketChannel讀取數(shù)據(jù)過程剖析

    這篇文章主要為大家介紹了Netty源碼分析ByteBuf使用SocketChannel讀取數(shù)據(jù)過程,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪
    2022-03-03
  • Java實戰(zhàn)之利用POI生成Excel圖表

    Java實戰(zhàn)之利用POI生成Excel圖表

    Apache POI是Java生態(tài)中處理Office文檔的核心工具,這篇文章主要為大家詳細介紹了如何在Excel中創(chuàng)建折線圖,柱狀圖,餅圖等常見圖表,需要的可以參考下
    2025-02-02
  • java 字符串反轉的實例詳解

    java 字符串反轉的實例詳解

    這篇文章主要介紹了java 字符串反轉的實例詳解的相關資料,這里提供實現(xiàn)代碼幫助大家學習參考這部分內容,需要的朋友可以參考下
    2017-08-08

最新評論

修水县| 明水县| 山西省| 敦煌市| 凌源市| 乐陵市| 时尚| 张家川| 军事| 河间市| 和林格尔县| 仪陇县| 乳源| 曲麻莱县| 临城县| 庆阳市| 汕头市| 京山县| 锡林浩特市| 怀来县| 百色市| 南康市| 同心县| 湖南省| 五河县| 宜宾县| 峨边| 贵溪市| 儋州市| 八宿县| 赤水市| 丰县| 桐乡市| 美姑县| 黄大仙区| 文水县| 荥阳市| 威信县| 武山县| 徐水县| 墨竹工卡县|