SpringMVC拦截器的应用场景:
1.解决乱码问题
2.解决权限验证问题
拦截器的实现
拦截器的实现主要分为3步:
第一步,编写拦截器类实现HandlerInterceptor接口。
第二步,将拦截器注册进SpringMVC框架。
第三步,配置拦截器的拦截规则
java实现拦截器类
public class MyInterceptor implements HandlerInterceptor{
/*
返回值:表示我们是否需要将当前的请求拦截下来
false:表示请求被终止
true表示请求会被继续执行
HttpServletRequest:包含所有请求的内容
HttpServletResponse:包含所有响应的内容
arg2:表示被拦截请求的目标对象
*/
@Overried
public boolean preHandle(HttpServletRequest arg0,HttpServletResponse arg1,Object arg2){
return true;
}
//可以通过ModeAndView参数来改变显示的视图,或者修改发往视图的方法
@Overried
public void postHandle(HttpServletRequest arg0,HttpServletResponse arg1,Exception arg3,Object arg2,ModelAndView arg3){
//将响应的地址改变到index.jsp
arg3.setViewName("/index.jsp");
}
//主要的功能是销毁
@Overried
public void afterCompletion(HttpServletRequest arg0,HttpServletResponse arg1,Object arg2,Exception arg3) throws Exception{
}
}
注册编写的拦截器
<!--注册拦截器-->
<mvc:interceptors>
<bean class="com.cn.springmvc.MyInterceptor"></bean>
</mvc:interceptors>
当我们编写好拦截器类,并且注册了这个类这个拦截器就可以使用了。接下来我们来配置一下拦截规则。
<!--注册拦截器-->
<mvc:interceptors>
<mvc:interceptor>
<!--拦截view.from结尾的请求-->
<mvc:mapping path="/view.from">
<bean class="com.cn.springmvc.MyInterceptor"></bean>
</mvc:interceptor>
</mvc:interceptors>
@controller
public class DemoController{
@requestMapping("view");
public ModeAndVide view(){
return "index.jsp";
}
}
解决乱码
public boolean preHandle(HttpServletRequest arg0,HttpServletResponse arg1,Object arg2){
argo.setCharaterEncoding("utf-8");
return true;
}
多个拦截器的实现
首先注册拦截器
//拦截器的注册
<mvc:interceptors>
<bean class="com.cn.mvc.MyInterceptor01"></bean>
<bean class="com.cn.mvc.MyInterceptor02"></bean>
</mvc:interceptors>
然后编写拦截器类
//拦截器类
public class MyInterceptor01 implements HandlerInterceptor{
@Overried
public boolean preHandle(HttpServletRequest arg0,HttpServletResponse arg1,Object arg2){
System.out.println("This is MyInterceptor01 preHandle");
return true;
}
@Overried
public void postHandle(HttpServletRequest arg0,HttpServletResponse arg1,Exception arg3,Object arg2,ModelAndView arg3){
System.out.println("This is MyInterceptor01 postHandle");
}
@Overried
public void afterCompletion(HttpServletRequest arg0,HttpServletResponse arg1,Object arg2,Exception arg3) throws Exception{
System.out.println("This is MyInterceptor01 afterCompletion");
}
}
//拦截器类
public class MyInterceptor02 implements HandlerInterceptor{
@Overried
public boolean preHandle(HttpServletRequest arg0,HttpServletResponse arg1,Object arg2){
System.out.println("This is MyInterceptor02 preHandle");
return true;
}
@Overried
public void postHandle(HttpServletRequest arg0,HttpServletResponse arg1,Exception arg3,Object arg2,ModelAndView arg3){
System.out.println("This is MyInterceptor02 postHandle");
}
@Overried
public void afterCompletion(HttpServletRequest arg0,HttpServletResponse arg1,Object arg2,Exception arg3) throws Exception{
System.out.println("This is MyInterceptor02 afterCompletion");
}
}
最后查看运行结果
This is MyInterceptor01 preHandle
This is MyInterceptor02 preHandle
...
...
This is MyInterceptor02 postHandle
This is MyInterceptor01 postHandle
This is MyInterceptor02 afterCompletion
This is MyInterceptor01 afterCompletion
我们可以从运行结果中看出两个拦截器的运行时,拦截器中方法的执行顺序。
拦截器的多种实现
1.继承AbstractInterceptor 只有拦截方法,默认初始胡和销毁实现
2.实现WebRequsetInterceptor 和实现HandlerInterceptor方法的唯一区别就是实现WebRequsetInterceptor的preHandle方法没有返回值,而实现HandlerInterceptor接口的preHandle方法具有返回值。
拦截器和过滤器的区别
过滤器Filter依赖于Servlet容器,基于回调函数,过滤范围大。
拦截器Interceptor依赖于框架容器,基于反射机制,只过滤请求。
网友评论