SpringMVC interceptors to configure and use

Disclaimer: This article is a blogger original article, follow the CC 4.0 BY-SA copyright agreement, reproduced, please attach the original source link and this statement.
This link: https://blog.csdn.net/qq262593421/article/details/100362265

                          SpringMVC interceptors to configure and use

Ideas:

  1. Priority loading spring-mvc.xml web.xml configuration when the front end controller initialization SpringMVC
  2. Use <mvc: interceptors> SpringMVC the xml configuration file in which the tag is added to bean interceptor class
  3. Write interceptor classes, inheritance extends HandlerInterceptorAdapter class, rewrite preHandle, postHandle method and afterCompletion

web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" version="2.5">
 
  <display-name>com</display-name>
  
  <welcome-file-list>
 	 <welcome-file>web/index.jsp</welcome-file>
  </welcome-file-list>
  
  	<!-- 利用web.xml的特性优先过滤静态资源  -->
	<servlet-mapping>
		<servlet-name>default</servlet-name>
		<url-pattern>*.js</url-pattern>
	</servlet-mapping>
	<servlet-mapping>
		<servlet-name>default</servlet-name>
		<url-pattern>*.css</url-pattern>
	</servlet-mapping>
	<servlet-mapping>
		<servlet-name>default</servlet-name>
		<url-pattern>*.html</url-pattern>
	</servlet-mapping>

	
  	<!-- 加载spring的配置文件 -->
	<context-param>
		<param-name>contextConfigLocation</param-name>
		<param-value>classpath:spring-core.xml</param-value>
	</context-param>

	<!-- spring web的监听器 IOC -->
	<listener>
		<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
	</listener>
	
	<!-- 开始配置过滤器 中文字符过滤器-->
	<filter>
		<filter-name>encodingFilter</filter-name>
		<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
		<init-param>
			<param-name>encoding</param-name>
			<param-value>UTF-8</param-value>
		</init-param>
	</filter>
	<filter-mapping>
		<filter-name>encodingFilter</filter-name>
		<url-pattern>/*</url-pattern>
	</filter-mapping>

	<!-- 配置springmvc的核心servlet -->
	<servlet>
		<servlet-name>dispatcherServlet</servlet-name>
		<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
		<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>
	<servlet-mapping>
		<servlet-name>dispatcherServlet</servlet-name>
		<url-pattern>/</url-pattern>
	</servlet-mapping>
	
</web-app>

spring-core.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:context="http://www.springframework.org/schema/context"
	xmlns:aop="http://www.springframework.org/schema/aop"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	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/aop
        http://www.springframework.org/schema/aop/spring-aop.xsd ">

	<!-- 1. 扫描需要依赖注入的注解bean -->
	<context:component-scan base-package="com.gxwz.code" />
	<context:component-scan base-package="com.gxwz.core" />

</beans>

spring-mvc.xml (key configuration files)

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:mvc="http://www.springframework.org/schema/mvc"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       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 ">

    <!-- web层操作 -->

    <!-- 1. 扫描具有HTTP协议的注解bean -->
    <context:component-scan base-package="com.gxwz.web" />

    <!-- 2. 开启springmvc的注解功能 -->
    <mvc:annotation-driven />
	
	<!-- 3. 使用springMVC的拦截器功能 -->
	<mvc:interceptors>
		<mvc:interceptor>
			<mvc:mapping path="/**"/>
			<bean class="com.gxwz.interceptor.IndexInterceptor"></bean>
		</mvc:interceptor>
	</mvc:interceptors>
	
    <!-- 3. 配置静态资源不被springmvc拦截  location:本地资源路径,注意必须是webapp根目录下的路径
	<mvc:resources mapping="/css/**" location="/web/static/h-ui/css/" />
    <mvc:resources mapping="/images/**" location="/web/static/h-ui/images/" />
    <mvc:resources mapping="/js/**" location="/web/static/h-ui/js/" /> -->
	
</beans>

IndexInterceptor.java

package com.gxwz.interceptor;

import java.util.Date;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
 
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;

/** 
* 在业务处理器处理请求之前被调用 
* 如果返回false 
*    从当前的拦截器往回执行所有拦截器的afterCompletion(),再退出拦截器链
* 如果返回true 
*    执行下一个拦截器,直到所有的拦截器都执行完毕 
*    再执行被拦截的Controller 
*    然后进入拦截器链, 
*    从最后一个拦截器往回执行所有的postHandle() 
*    接着再从最后一个拦截器往回执行所有的afterCompletion() 
*    与过滤器的区别
*    1.过滤器是依赖于Servlet容器,基于回调函数,Interceptor依赖与框架,基于反射机制
*    2.过滤器的过滤范围更大,还可以过滤一些静态资源,拦截器只拦截请求
*/  
public class IndexInterceptor extends HandlerInterceptorAdapter { 
 
	@Override
    public boolean preHandle(HttpServletRequest request,HttpServletResponse response, 
    		Object handler) throws Exception {
		//业务逻辑代码编写...(如:解决乱码,权限验证)
        System.out.println("preHandle(), 在访问Controller之前被调用"); 
        return true;
    } 
    /**
     * 在业务处理器处理请求执行完成后,生成视图之前执行的动作   
     * 可在modelAndView中加入数据,比如当前时间
     */ 
	@Override
    public void postHandle(HttpServletRequest request,HttpServletResponse response, Object handler,   
            ModelAndView modelAndView) throws Exception { 
		//业务逻辑代码编写...(如:操作日志记录,更改视图信息)
		System.out.println("postHandle(), 在访问Controller之后,访问视图之前被调用,这里可以注入一个时间到modelAndView中,用于后续视图显示");
        modelAndView.addObject("date","由拦截器生成的时间:" + new Date());
    } 
    /** 
     * 在DispatcherServlet完全处理完请求后被调用,可用于清理资源等  
     * 当有拦截器抛出异常时,会从当前拦截器往回执行所有的拦截器的afterCompletion() 
     */
	@Override
    public void afterCompletion(HttpServletRequest request,HttpServletResponse response, 
    		Object handler, Exception ex) throws Exception { 
		//业务逻辑代码编写...(如:,资源销毁,异常处理)
        System.out.println("afterCompletion(), 在访问视图之后被调用"); 
    } 
       
}

 

Guess you like

Origin blog.csdn.net/qq262593421/article/details/100362265