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

Spring mvc攔截器實(shí)現(xiàn)原理解析

 更新時(shí)間:2020年03月19日 12:42:57   作者:黃大姐の老公  
這篇文章主要介紹了Spring mvc攔截器實(shí)現(xiàn)原理解析,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下

概述

SpringMVC的處理器攔截器類似于Servlet開發(fā)中的過濾器Filter,用于對處理器進(jìn)行預(yù)處理和后處理。開發(fā)者可以自己定義一些攔截器來實(shí)現(xiàn)特定的功能。

過濾器與攔截器的區(qū)別:攔截器是AOP思想的具體應(yīng)用。

過濾器

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

攔截器

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

自定義攔截器

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

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

新建一個(gè)Moudule , 添加web支持

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

編寫一個(gè)攔截器

package com.xiaohua.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 {

  //在請求處理的方法之前執(zhí)行
  //如果返回true執(zhí)行下一個(gè)攔截器
  //如果返回false就不執(zhí)行下一個(gè)攔截器
  public boolean preHandle(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o) throws Exception {
    System.out.println("------------處理前------------");
    return true;
  }

  //在請求處理方法執(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("------------清理------------");
  }
}

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

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

編寫一個(gè)Controller,接收請求

package com.xiaohua.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

//測試攔截器的控制器
@Controller
public class InterceptorController {

  @RequestMapping("/interceptor")
  @ResponseBody
  public String testFunction() {
    System.out.println("控制器中的方法執(zhí)行了");
    return "hello";
  }
}

前端 index.jsp

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

啟動(dòng)tomcat 測試一下!

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

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

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

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

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

代碼編寫

編寫一個(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>

編寫一個(gè)Controller處理請求

package com.xiaohua.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";
  }
}

編寫一個(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>

在 index 頁面上測試跳轉(zhuǎn)!啟動(dòng)Tomcat 測試,未登錄也可以進(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>

編寫用戶登錄攔截器

package com.xiaohua.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 {
  }
}

在Springmvc的配置文件中注冊攔截器

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

再次重啟Tomcat測試!

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • SpringBoot集成Zipkin實(shí)現(xiàn)分布式全鏈路監(jiān)控

    SpringBoot集成Zipkin實(shí)現(xiàn)分布式全鏈路監(jiān)控

    這篇文章主要介紹了SpringBoot集成Zipkin實(shí)現(xiàn)分布式全鏈路監(jiān)控的方法啊,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2019-09-09
  • Spring中的@DependsOn注解使用解析

    Spring中的@DependsOn注解使用解析

    這篇文章主要介紹了Spring中的@DependsOn注解使用解析,@DependsOn注解可以定義在類和方法上,意思是我這個(gè)組件要依賴于另一個(gè)組件,也就是說被依賴的組件會(huì)比該組件先注冊到IOC容器中,需要的朋友可以參考下
    2024-01-01
  • MyBatis-plus更新對象時(shí)將字段值更新為null的實(shí)現(xiàn)方式

    MyBatis-plus更新對象時(shí)將字段值更新為null的實(shí)現(xiàn)方式

    mybatis-plus在執(zhí)行更新操作,當(dāng)更新字段為 空字符串 或者 null 的則不會(huì)執(zhí)行更新,如果要將指定字段更新null,可以通過以下三種方式實(shí)現(xiàn),感興趣的小伙伴跟著小編一起來看看吧
    2023-10-10
  • java 定時(shí)同步數(shù)據(jù)的任務(wù)優(yōu)化

    java 定時(shí)同步數(shù)據(jù)的任務(wù)優(yōu)化

    這篇文章主要介紹了java 定時(shí)同步數(shù)據(jù)的任務(wù)優(yōu)化,幫助大家更好的理解和使用Java,感興趣的朋友可以了解下
    2020-12-12
  • java字符串與格式化輸出的深入分析

    java字符串與格式化輸出的深入分析

    本篇文章是對java字符串與格式化輸出進(jìn)行了詳細(xì)的分析介紹,需要的朋友參考下
    2013-06-06
  • 如何將文件流轉(zhuǎn)換成byte[]數(shù)組

    如何將文件流轉(zhuǎn)換成byte[]數(shù)組

    這篇文章主要介紹了如何將文件流轉(zhuǎn)換成byte[]數(shù)組,具有很好的參考價(jià)值,希望對大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2021-12-12
  • java 流操作對文件的分割和合并的實(shí)例詳解

    java 流操作對文件的分割和合并的實(shí)例詳解

    這篇文章主要介紹了java 流操作對文件的分割和合并的實(shí)例詳解的相關(guān)資料,希望通過本文能幫助到大家,讓大家理解掌握這部分內(nèi)容,需要的朋友可以參考下
    2017-10-10
  • Spring Boot 熱部署實(shí)現(xiàn)步驟詳解

    Spring Boot 熱部署實(shí)現(xiàn)步驟詳解

    文章介紹了如何在IntelliJ IDEA中實(shí)現(xiàn)SpringBoot項(xiàng)目的熱部署功能,包括開啟自動(dòng)編譯、運(yùn)行時(shí)自動(dòng)更新、添加熱部署依賴以及配置選項(xiàng)等步驟,并鼓勵(lì)讀者分享其他實(shí)現(xiàn)方式,感興趣的朋友跟隨小編一起看看吧
    2025-02-02
  • IDEA使用SequenceDiagram插件繪制時(shí)序圖的方法

    IDEA使用SequenceDiagram插件繪制時(shí)序圖的方法

    這篇文章主要介紹了IDEA使用SequenceDiagram插件繪制時(shí)序圖的方法,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2020-09-09
  • Java中實(shí)現(xiàn)文件上傳下載的三種解決方案(推薦)

    Java中實(shí)現(xiàn)文件上傳下載的三種解決方案(推薦)

    這篇文章主要介紹了Java中實(shí)現(xiàn)文件上傳下載的三種解決方案的相關(guān)資料,非常不錯(cuò),具有參考借鑒價(jià)值,需要的朋友可以參考下
    2016-07-07

最新評論

安乡县| 五指山市| 静乐县| 逊克县| 扶绥县| 岫岩| 巴马| 凤庆县| 类乌齐县| 石家庄市| 宝山区| 海盐县| 温州市| 绥棱县| 新竹县| 府谷县| 沙坪坝区| 孟州市| 临安市| 晋中市| 庆城县| 黄石市| 锡林郭勒盟| 镇江市| 从江县| 济宁市| 屏边| 平果县| 吴桥县| 若尔盖县| 崇仁县| 葫芦岛市| 苍梧县| 固安县| 南城县| 庆阳市| 平泉县| 兰考县| 延安市| 齐齐哈尔市| 舒兰市|