ssm框架拦截器配置(配置文件方式 和 纯注解方式)
2022-12-21 本文已影响0人
晓晓_1931
项目整体搭建基于以下文章:
ssm框架整合(配置文件方式 和 纯注解方式) - 简书 (jianshu.com)
无论那种方式,都需要创建一个自定义拦截器类
package com.xx.ssm.interceptor;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;
public class IndexInterceptor implements HandlerInterceptor{
@Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
System.out.println("该方法在 controller 执行前执行,可以实现对数据的预处理。 如果方法返回 true ,则继续调用下一个资源。否则不在继续调用。");
return false;//为false是拦截,true是放行
}
@Override
public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler,ModelAndView modelAndView) throws Exception {
System.out.println("该方法在处理器执行后,生成视图前执行。这里有机会修改视图层数据");
}
@Override
public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex)throws Exception {
System.out.println("最后执行,通常用于记录日志,释放资源,处理异常。");
}
}
一、配置文件方式
<?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: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/aop
http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc.xsd">
<!-- 自动加载注解的处理器映射和注解的处理器适配 -->
<mvc:annotation-driven />
<!-- 开启注解扫描,将包下带有@Controller注解的类纳入Spring容器中-->
<context:component-scan base-package="com.xx.ssm.controller" />
<!-- 可以配置多个拦截器 -->
<mvc:interceptors>
<mvc:interceptor>
<mvc:mapping path="/**" /><!-- 拦截所有路径及其子路径-->
<mvc:exclude-mapping path="/login" /><!-- 放行的路径-->
<bean class="com.xx.ssm.interceptor.IndexInterceptor" /> <!-- 注册自定义拦截器类-->
</mvc:interceptor>
</mvc:interceptors>
</beans>
二、纯注解方式
修改WebConfig类
package com.neusoft.ssm;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
import com.xx.ssm.interceptor.IndexInterceptor;
@Configuration
@EnableWebMvc // SpringMVC注解,等同于<mvc:annotation-driven/>
@ComponentScan(basePackages = { "com.neusoft.ssm.controller" }) // @ComponentScan用于扫描通用组件,这里用来扫描Controller组件
public class WebConfig implements WebMvcConfigurer { //实现WebMvcConfigurer接口
@Bean //将自定义拦截器放入到spring容器中
public IndexInterceptor interceptor() {
return new IndexInterceptor();
}
@Autowired //创建自定义的拦截器
IndexInterceptor interceptor;
@Override
public void addInterceptors(InterceptorRegistry registry) {
registry.addInterceptor(interceptor) // 添加拦截器
.addPathPatterns("/**") // 添加需要拦截的路径
.excludePathPatterns("/login"); // 放行的路径
}
}