欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

SpringMVC的自定義攔截器詳解

 更新時(shí)間:2023年07月18日 10:53:36   作者:胡小冰  
這篇文章主要介紹了SpringMVC的自定義攔截器詳解,攔截器只會(huì)攔截訪問的控制器方法, 如果訪問的是jsp/html/css/image/js是不會(huì)進(jìn)行攔截的,需要的朋友可以參考下

過濾器與攔截器的區(qū)別

攔截器是AOP思想的具體應(yīng)用。

過濾器

  • servlet規(guī)范中的一部分,任何java web工程都可以使用
  • 在url-pattern中配置了/*之后,可以對(duì)所有要訪問的資源進(jìn)行攔截

攔截器

  • 攔截器是SpringMVC框架自己的,只有使用了SpringMVC框架的工程才能使用
  • 攔截器只會(huì)攔截訪問的控制器方法, 如果訪問的是jsp/html/css/image/js是不會(huì)進(jìn)行攔截的

自定義攔截器

那如何實(shí)現(xiàn)攔截器呢?

想要自定義攔截器,必須實(shí)現(xiàn) HandlerInterceptor 接口。

1、新建一個(gè)Moudule , springmvc-07-Interceptor , 添加web支持

2、配置web.xml 和 springmvc-servlet.xml 文件

3、編寫一個(gè)攔截器

package com.kuang.interceptor;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class MyInterceptor implements HandlerInterceptor {
   //在請(qǐng)求處理的方法之前執(zhí)行
   //如果返回true執(zhí)行下一個(gè)攔截器
   //如果返回false就不執(zhí)行下一個(gè)攔截器
   public boolean preHandle(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o) throws Exception {
       System.out.println("------------處理前------------");
       return true;
  }
   //在請(qǐng)求處理方法執(zhí)行之后執(zhí)行
   public void postHandle(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o, ModelAndView modelAndView) throws Exception {
       System.out.println("------------處理后------------");
  }
   //在dispatcherServlet處理后執(zhí)行,做清理工作.
   public void afterCompletion(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o, Exception e) throws Exception {
       System.out.println("------------清理------------");
  }
}

4、在springmvc的配置文件中配置攔截器

<!--關(guān)于攔截器的配置-->
<mvc:interceptors>
   <mvc:interceptor>
       <!--/** 包括路徑及其子路徑-->
       <!--/admin/* 攔截的是/admin/add等等這種 , /admin/add/user不會(huì)被攔截-->
       <!--/admin/** 攔截的是/admin/下的所有-->
       <mvc:mapping path="/**"/>
       <!--bean配置的就是攔截器-->
       <bean class="com.kuang.interceptor.MyInterceptor"/>
   </mvc:interceptor>
</mvc:interceptors>

5、編寫一個(gè)Controller,接收請(qǐng)求

package com.kuang.controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
//測(cè)試攔截器的控制器
@Controller
public class InterceptorController {
   @RequestMapping("/interceptor")
   @ResponseBody
   public String testFunction() {
       System.out.println("控制器中的方法執(zhí)行了");
       return "hello";
  }
}

6、前端 index.jsp

<a href="${pageContext.request.contextPath}/interceptor" rel="external nofollow" >攔截器測(cè)試</a>

7、啟動(dòng)tomcat 測(cè)試一下!

驗(yàn)證用戶是否登錄 (認(rèn)證用戶)

實(shí)現(xiàn)思路

1、有一個(gè)登陸頁面,需要寫一個(gè)controller訪問頁面。

2、登陸頁面有一提交表單的動(dòng)作。需要在controller中處理。判斷用戶名密碼是否正確。如果正確,向session中寫入用戶信息。返回登陸成功。

3、攔截用戶請(qǐng)求,判斷用戶是否登陸。如果用戶已經(jīng)登陸。放行, 如果用戶未登陸,跳轉(zhuǎn)到登陸頁面

測(cè)試:

1、編寫一個(gè)登陸頁面 login.jsp

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
   <title>Title</title>
</head>
<h1>登錄頁面</h1>
<hr>
<body>
<form action="${pageContext.request.contextPath}/user/login">
  用戶名:<input type="text" name="username"> <br>
  密碼:<input type="password" name="pwd"> <br>
   <input type="submit" value="提交">
</form>
</body>
</html>

2、編寫一個(gè)Controller處理請(qǐng)求

package com.kuang.controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import javax.servlet.http.HttpSession;
@Controller
@RequestMapping("/user")
public class UserController {
   //跳轉(zhuǎn)到登陸頁面
   @RequestMapping("/jumplogin")
   public String jumpLogin() throws Exception {
       return "login";
  }
   //跳轉(zhuǎn)到成功頁面
   @RequestMapping("/jumpSuccess")
   public String jumpSuccess() throws Exception {
       return "success";
  }
   //登陸提交
   @RequestMapping("/login")
   public String login(HttpSession session, String username, String pwd) throws Exception {
       // 向session記錄用戶身份信息
       System.out.println("接收前端==="+username);
       session.setAttribute("user", username);
       return "success";
  }
   //退出登陸
   @RequestMapping("logout")
   public String logout(HttpSession session) throws Exception {
       // session 過期
       session.invalidate();
       return "login";
  }
}

3、編寫一個(gè)登陸成功的頁面 success.jsp

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
   <title>Title</title>
</head>
<body>
<h1>登錄成功頁面</h1>
<hr>
${user}
<a href="${pageContext.request.contextPath}/user/logout" rel="external nofollow" >注銷</a>
</body>
</html>

4、在 index 頁面上測(cè)試跳轉(zhuǎn)!啟動(dòng)Tomcat 測(cè)試,未登錄也可以進(jìn)入主頁!

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
 <head>
   <title>$Title$</title>
 </head>
 <body>
 <h1>首頁</h1>
 <hr>
<%--登錄--%>
 <a href="${pageContext.request.contextPath}/user/jumplogin" rel="external nofollow" >登錄</a>
 <a href="${pageContext.request.contextPath}/user/jumpSuccess" rel="external nofollow" >成功頁面</a>
 </body>
</html>

5、編寫用戶登錄攔截器

package com.kuang.interceptor;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
public class LoginInterceptor implements HandlerInterceptor {
   public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws ServletException, IOException {
       // 如果是登陸頁面則放行
       System.out.println("uri: " + request.getRequestURI());
       if (request.getRequestURI().contains("login")) {
           return true;
      }
       HttpSession session = request.getSession();
       // 如果用戶已登陸也放行
       if(session.getAttribute("user") != null) {
           return true;
      }
       // 用戶沒有登陸跳轉(zhuǎn)到登陸頁面
       request.getRequestDispatcher("/WEB-INF/jsp/login.jsp").forward(request, response);
       return false;
  }
   public void postHandle(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o, ModelAndView modelAndView) throws Exception {
  }
   public void afterCompletion(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o, Exception e) throws Exception {
  }
}

6、在Springmvc的配置文件中注冊(cè)攔截器

<!--關(guān)于攔截器的配置-->
<mvc:interceptors>
   <mvc:interceptor>
       <mvc:mapping path="/**"/>
       <bean id="loginInterceptor" class="com.kuang.interceptor.LoginInterceptor"/>
   </mvc:interceptor>
</mvc:interceptors>

7、再次重啟Tomcat測(cè)試!

到此這篇關(guān)于SpringMVC的自定義攔截器詳解的文章就介紹到這了,更多相關(guān)SpringMVC攔截器內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • Mybatis-Plus打印sql日志兩種方式

    Mybatis-Plus打印sql日志兩種方式

    這篇文章主要給大家介紹了關(guān)于Mybatis-Plus打印sql日志兩種方式,Mybatis-plus是MyBatis增強(qiáng)工具包,用于簡(jiǎn)化CRUD操作,文中通過示例代碼介紹的非常詳細(xì),需要的朋友可以參考下
    2023-07-07
  • Java項(xiàng)目中如何訪問WEB-INF下jsp頁面

    Java項(xiàng)目中如何訪問WEB-INF下jsp頁面

    這篇文章主要介紹了Java項(xiàng)目中如何訪問WEB-INF下jsp頁面,文章通過示例代碼和圖文解析介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2020-08-08
  • spring?boot項(xiàng)目中如何使用nacos作為配置中心

    spring?boot項(xiàng)目中如何使用nacos作為配置中心

    這篇文章主要介紹了spring?boot項(xiàng)目中如何使用nacos作為配置中心問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2022-12-12
  • java文件上傳技術(shù)深入剖析

    java文件上傳技術(shù)深入剖析

    這篇文章主要為大家詳細(xì)介紹了java文件上傳技術(shù)深入剖析,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2016-10-10
  • Java面試最容易被刷的重難點(diǎn)之鎖的使用策略

    Java面試最容易被刷的重難點(diǎn)之鎖的使用策略

    鎖像synchronized同步塊一樣,是一種線程同步機(jī)制,但比Java中的synchronized同步塊更復(fù)雜。因?yàn)殒i(以及其它更高級(jí)的線程同步機(jī)制)是由synchronized同步塊的方式實(shí)現(xiàn)的,所以我們還不能完全擺脫synchronized關(guān)鍵字
    2021-10-10
  • 淺析Java中線程的創(chuàng)建和啟動(dòng)

    淺析Java中線程的創(chuàng)建和啟動(dòng)

    這篇文章運(yùn)用實(shí)例代碼介紹了Java中線程的創(chuàng)建和啟動(dòng),非常詳細(xì),有需要的朋友們可以參考借鑒,下面一起來看看。
    2016-08-08
  • Apache DolphinScheduler完全設(shè)置東八區(qū)時(shí)區(qū)

    Apache DolphinScheduler完全設(shè)置東八區(qū)時(shí)區(qū)

    這篇文章主要為大家介紹了Apache DolphinScheduler完全設(shè)置東八區(qū)配置詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2023-11-11
  • eclipse啟動(dòng)一個(gè)Springboot項(xiàng)目

    eclipse啟動(dòng)一個(gè)Springboot項(xiàng)目

    本文主要介紹了eclipse啟動(dòng)一個(gè)Springboot項(xiàng)目,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2022-08-08
  • Java實(shí)現(xiàn)簡(jiǎn)單掃雷程序

    Java實(shí)現(xiàn)簡(jiǎn)單掃雷程序

    這篇文章主要為大家詳細(xì)介紹了Java實(shí)現(xiàn)簡(jiǎn)單掃雷程序,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2022-06-06
  • 如何解決Springboot?Dao注入失敗的問題

    如何解決Springboot?Dao注入失敗的問題

    這篇文章主要介紹了如何解決Spring?boot?Dao注入失敗的問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2024-05-05

最新評(píng)論