Spring整合MVC之拦截器
项目结构:
1. 先编写SpringMvcConfig配置类
@ComponentScan({"com.itheima.controller", "com.itheima.config"})
@EnableWebMvc
public class SpringMvcConfig {
}
# 2. 然后编写ServletConfigInitializer配置类,//加载SpringMVC配置文件,子容器,子容器可以调用父容器中对象
```java
/**
* 这个类会在tomcat启动的时候加载
*/
public class ServletConfigInitializer extends AbstractAnnotationConfigDispatcherServletInitializer {
//加载Spring配置文件,父容器
@Override
protected Class<?>[] getRootConfigClasses() {
return new Class[0];
}
//加载SpringMVC配置文件,子容器,子容器可以调用父容器中对象
@Override
protected Class<?>[] getServletConfigClasses() {
return new Class[] {SpringMvcConfig.class};
}
//拦截所有的地址
@Override
protected String[] getServletMappings() {
return new String[] {"/"};
}
}
3.编写两个拦截器,
/**
* 最终增强:无论处理器方法中是否有异常,都会执行
* @param ex 如果处理器中抛出了异常,可以获取到那个异常
*/
@Override/**
* 拦截器1
*/
public class BookInterceptor implements HandlerInterceptor {
/**
* 前置增强:在处理器的方法前执行
* @param request 请求
* @param response 响应
* @param handler 可以获取处理器BookController中信息
* @return 返回true,放行,否则就拦截
*/
@Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
System.out.println("前置增强1");
//HandlerMethod handlerMethod = (HandlerMethod) handler;
//Method method = handlerMethod.getMethod();
//System.out.println("处理器中拦截的方法:" + method);
return true;
}
/**
* 后置增强:如果处理器中的方法抛出异常,则后置增强不执行
* @param modelAndView 封装模型和视图的对象
*/
@Override
public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {
System.out.println("后置增强1");
}
public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {
//if (ex != null) {
// System.out.println("出现了异常:" + ex.getMessage());
//}
System.out.println("最终增强1");
}
}
/**
* 第二个拦截器
*/
public class Book2Interceptor implements HandlerInterceptor{
@Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
System.out.println("前置增强2");
return false;
}
@Override
public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {
System.out.println("后置增强2");
}
@Override
public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {
System.out.println("最终增强2");
}
}
4.添加拦截器的功能,编写SpringMvcSupport类
/**
* 添加拦截器的功能
*/
@Configuration
public class SpringMvcSupport extends WebMvcConfigurationSupport {
//添加拦截器
@Override
protected void addInterceptors(InterceptorRegistry registry) {
//1.添加拦截器 2.指定拦截的地址
registry.addInterceptor(new BookInterceptor()).addPathPatterns("/book/*");
//添加第2个拦截器
registry.addInterceptor(new Book2Interceptor()).addPathPatterns("/book/*");
}
}
5.编写控制类 BookController
@RestController //@Controller + @ResponseBody
@RequestMapping("/book") //模块访问地址
public class BookController {
/**
* 查询书籍
*/
@GetMapping("/find")
public String find() {
System.out.println("查找书籍");
//模拟出现异常
//System.out.println(1 / 0);
return "find success";
}
}