9.SpringMvc拦截器-创新互联
1. 拦截器概念和作用
文章名称:9.SpringMvc拦截器-创新互联
分享网址:http://scyanting.com/article/dedipp.html
- 拦截器(Interceptor)是一种动态拦截方法调用的机制,在SpringMVC中动态拦截控制器方法的执行
- 作用:
- 在指定的方法调用前后执行预先设定的代码
- 阻止原始方法的执行
- 总结:增强
- 核心原理:AOP思想
- 归属不同:Filter属于Servlet技术,Interceptor属于SpringMVC技术
- 拦截内容不同:Filter对所有访问进行增强,Interceptor仅针对SpringMVC的访问进行增强
package com.zhang.controller.interceptor;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
//注意当前类必须受Spring容器控制
@Component//定义拦截器类,实现HandlerInterceptor接口
public class ProjectInterceptor implements HandlerInterceptor { //原始方法调用前执行的内容
//返回值类型可以拦截控制的执行,true放行,false终止
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {System.out.println("Controller执行前面");
return true;
}
//原始方法调用后执行的内容
public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {System.out.println("Controller执行后面");
}
//原始方法调用完成后执行的内容
public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception { System.out.println("Controller执行完成后");
}
}
3.2 配置加载拦截器,定义一个类继承 WebMvcConfigurationSupport这个类注意要被SpringMvcConfig配置类扫描到,不然不生效
package com.zhang.config;
import com.zhang.controller.interceptor.ProjectInterceptor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport;
@Configuration
public class MvcSupportConfig extends WebMvcConfigurationSupport {@Autowired
private ProjectInterceptor projectInterceptor;
protected void addInterceptors(InterceptorRegistry registry) {registry.addInterceptor(projectInterceptor).addPathPatterns("/**");//拦截所有请求
}
}
3.3 Controllerpackage com.zhang.controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
@RequestMapping("/books")
public class BookController {@GetMapping()
public String save(){System.out.println("正在执行Controller:"+"Book...Save");
return "Hello SpringMvc";
}
}
3.4 posatman发送请求,后台打印结果4. 拦截器流程分析5. 拦截器参数
5.1 前置处理//原始方法调用前执行的内容
//返回值类型可以拦截控制的执行,true放行,false终止
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {System.out.println("preHandle..."+contentType);
return true;
}
参数
- request:请求对象
- response:响应对象
- handler:被调用的处理器对象,本质上是一个方法对象,对反射技术中的Method对象进行了再包装
返回值
返回值为false,后面的Controller方法不执行
//原始方法调用后执行的内容
public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {System.out.println("postHandle...");
}
- 参数
modelAndView:如果处理器执行完成具有返回结果,可以读取到对应数据与页面信息,并进行跳转
5.3 完成后处理注意:如果处理器方法出现异常了,该方法不会执行
//原始方法调用完成后执行的内容
public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {System.out.println("afterCompletion...");
}
参数
ex:如果处理器执行过程中出现异常对象,可以针对异常情况进行单独处理
6. 拦截器链配置 6.1 多个拦截器配置注意:无论处理器方法内部是否出现异常,该方法都会执行。
定义第二个拦截器
@Component
public class ProjectInterceptor2 implements HandlerInterceptor {@Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {System.out.println("preHandle...222");
return false;
}
@Override
public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {System.out.println("postHandle...222");
}
@Override
public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {System.out.println("afterCompletion...222");
}
}
配置第二个拦截器
package com.zhang.config;
import com.zhang.controller.interceptor.ProjectInterceptor;
import com.zhang.controller.interceptor.ProjectInterceptor2;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport;
@Configuration
public class MvcSupportConfig extends WebMvcConfigurationSupport {@Autowired
private ProjectInterceptor2 projectInterceptor2;
@Autowired
private ProjectInterceptor projectInterceptor;
protected void addInterceptors(InterceptorRegistry registry) {registry.addInterceptor(projectInterceptor).addPathPatterns("/**");//拦截所有请求
registry.addInterceptor(projectInterceptor2).addPathPatterns("/**");
}
}
6.2 多个连接器工作流程分析- 当配置多个拦截器时,形成拦截器链
- 拦截器链的运行顺序参照拦截器添加顺序为准
- 当拦截器中出现对原始处理器的拦截,后面的拦截器均终止运行
- 当拦截器运行中断,仅运行配置在前面的拦截器的afterCompletion操作
你是否还在寻找稳定的海外服务器提供商?创新互联www.cdcxhl.cn海外机房具备T级流量清洗系统配攻击溯源,准确流量调度确保服务器高可用性,企业级服务器适合批量采购,新人活动首月15元起,快前往官网查看详情吧
文章名称:9.SpringMvc拦截器-创新互联
分享网址:http://scyanting.com/article/dedipp.html