SpringMVC攔截器詳解
更新時間:2021年07月17日 09:38:12 作者:池魚i_
本篇文章主要介紹了SpringMVC攔截器配置及使用方法,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
web.xml文件
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
version="4.0">
<!-- 配置springMvc中央處理器-->
<servlet>
<servlet-name>springMVC</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<!-- 初始化的時候配置加載springmvc的配置文件-->
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:spring-mvc.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<!-- 3、修改web.xml中的請求路徑地址方式-->
<servlet-mapping>
<servlet-name>springMVC</servlet-name>
<!-- 3、修改web.xml中的請求路徑地址方式-->
<url-pattern>/</url-pattern>
<!-- <url-pattern>*.do</url-pattern>-->
</servlet-mapping>
<!--
1、將css文件使用默認(rèn)處理方式進(jìn)行處理,不再交由中央控制器處理,
設(shè)置路徑的更高匹配優(yōu)先級
-->
<!-- <servlet-mapping>-->
<!-- <servlet-name>default</servlet-name>-->
<!-- <url-pattern>*.css</url-pattern>-->
<!-- <url-pattern>*.js</url-pattern>-->
<!-- <url-pattern>*.jpg</url-pattern>-->
<!-- <url-pattern>*.png</url-pattern>-->
<!-- <url-pattern>*.html</url-pattern>-->
<!-- </servlet-mapping>-->
</web-app>
springMVC配置文件:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc.xsd">
<!--自動掃描包-->
<!-- 開啟ioc 注解事務(wù)支持-->
<context:component-scan base-package="cn"></context:component-scan>
<!--開啟spiring mvc注解支持-->
<mvc:annotation-driven></mvc:annotation-driven>
<!--配置spring 中的視圖解析器-->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver" id="resolver">
<property name="prefix" value="/"></property>
<property name="suffix" value=".jsp"></property>
</bean>
<mvc:interceptors>
<bean id="interceptor1" class="cn.hp.interceptor.TestInterceptor1"></bean>
<mvc:interceptor>
<mvc:mapping path="/**"/>
<bean id="interceptor1" class="cn.hp.interceptor.TestInterceptor1"></bean>
</mvc:interceptor>
<mvc:interceptor>
<mvc:mapping path="/test1.do"/>
<mvc:exclude-mapping path="/test2.do"/>
<bean id="interceptor2" class="cn.hp.interceptor.TestInterceptor2"></bean>
</mvc:interceptor>
</beans>
controller類:
package cn.hp.action;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import javax.servlet.http.HttpSession;
@Controller
public class UserAction {
@RequestMapping("/test1.do")
public String test01(){
System.out.println("正在執(zhí)行test1這個業(yè)務(wù)邏輯");
return "index";
}
@RequestMapping("/test2.do")
public String test02(){
System.out.println("正在執(zhí)行test2這個業(yè)務(wù)邏輯");
return "index";
}
}
第一個攔截器配置:
package cn.hp.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 TestInterceptor1 implements HandlerInterceptor {
@Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
System.out.println("第一個攔截器,執(zhí)行業(yè)務(wù)邏輯之前執(zhí)行此方法");
return true;
}
@Override
public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {
System.out.println("第一個攔截器,執(zhí)行業(yè)務(wù)邏輯之后,視圖渲染之前執(zhí)行此方法");
}
@Override
public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {
System.out.println("第一個攔截器,執(zhí)行視圖渲染之后執(zhí)行此方法");
}
}
第二個攔截器:
package cn.hp.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 TestInterceptor2 implements HandlerInterceptor {
@Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
System.out.println("第二個攔截器,執(zhí)行業(yè)務(wù)邏輯之前執(zhí)行此方法");
return true;
}
@Override
public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {
System.out.println("第二個攔截器,執(zhí)行業(yè)務(wù)邏輯之后,視圖渲染之前執(zhí)行此方法");
}
@Override
public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {
System.out.println("第二個攔截器,執(zhí)行視圖渲染之后執(zhí)行此方法");
}
}

總結(jié)
本篇文章就到這里了,希望能給你帶來幫助,也希望您能夠多多關(guān)注腳本之家的更多內(nèi)容!
相關(guān)文章
springboot文件上傳時maxPostSize設(shè)置大小失效問題及解決
這篇文章主要介紹了springboot文件上傳時maxPostSize設(shè)置大小失效問題及解決方案,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2023-07-07
使用SpringBoot-JPA進(jìn)行自定義保存及批量保存功能
這篇文章主要介紹了使用SpringBoot-JPA進(jìn)行自定義的保存及批量保存功能,本文通過實例代碼給大家介紹的非常詳細(xì),具有一定的參考借鑒價值,需要的朋友可以參考下2019-06-06
springboot如何讀取application.yml文件
這篇文章主要介紹了springboot如何讀取application.yml文件的方法,幫助大家更好的理解和使用springboot框架,感興趣的朋友可以了解下2020-12-12
MyBatis實現(xiàn)多表聯(lián)合查詢resultType的返回值
這篇文章主要介紹了MyBatis多表聯(lián)合查詢resultType的返回值,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-03-03
SpringBoot利用MDC機(jī)制過濾單次請求的所有日志
在服務(wù)出現(xiàn)故障時,我們經(jīng)常需要獲取一次請求流程里的所有日志進(jìn)行定位 ,如何將一次數(shù)據(jù)上報請求中包含的所有業(yè)務(wù)日志快速過濾出來,就是本文要介紹的,需要的朋友可以參考下2024-04-04

