浅谈springMVC拦截器和过滤器总结
拦截器:用来对访问的url进行拦截处理
用处:权限验证,乱码设置等
spring-mvc.xml文件中的配置:
<beansxmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p" xmlns:context="http://www.springframework.org/schema/context" xmlns:mvc="http://www.springframework.org/schema/mvc" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.1.xsd http://www.springframework.org/schema/tx ">
<!--编写拦截器--> <mvc:interceptors> <!--对特定的url拦截--> <mvc:interceptor> <mvc:mappingpath="/test.do"/> <beanclass="com.hbut.interceptor.TestInterceptor"/> </mvc:interceptor> <mvc:interceptor> <!--对特定的模块拦截第一级别拦截--> <mvc:mappingpath="/test/×/"/> <beanclass="com.hbut.interceptor.TestInterceptor1"/> </mvc:interceptor> <mvc:interceptor> <!--对特定的模块拦截--> <mvc:mappingpath="/test/×"/> <beanclass="com.hbut.interceptor.TestInterceptor2"/> </mvc:interceptor> </mvc:interceptors>
对所有的url进行拦截
<mvc:interceptors> <beanclass="org.springframework.web.servlet.i18n.LocaleChangeInterceptor"/> </mvc:interceptors>
java代码
packagecom.hbut.interceptor; importorg.springframework.web.servlet.HandlerInterceptor; importorg.springframework.web.servlet.ModelAndView; importjavax.servlet.http.HttpServletRequest; importjavax.servlet.http.HttpServletResponse; /** *@AuthorXiJun.Gong *@DATE2016/6/1. *aim:com.hbut.interceptor */ publicclassTestInterceptorimplementsHandlerInterceptor{ @Override publicbooleanpreHandle(HttpServletRequesthttpServletRequest,HttpServletResponsehttpServletResponse,Objecto)throwsException{ //todo在此处添加要操作code System.out.println("preHandle"); returntrue;//todo此处为false时,请求不会到达control层 } @Override publicvoidpostHandle(HttpServletRequesthttpServletRequest,HttpServletResponsehttpServletResponse,Objecto,ModelAndViewmodelAndView)throwsException{ System.out.println("postHandle");//todo可以用来修改信息,跳转等 } @Override publicvoidafterCompletion(HttpServletRequesthttpServletRequest,HttpServletResponsehttpServletResponse,Objecto,Exceptione)throwsException{ System.out.println("afterCompletion");//todo最后执行 } }
另一种拦截器:大同小异
packagecom.hbut.interceptor; importorg.springframework.ui.ModelMap; importorg.springframework.web.context.request.WebRequest; importorg.springframework.web.context.request.WebRequestInterceptor; /** *@AuthorXiJun.Gong *@DATE2016/6/1. *aim:com.hbut.interceptor */ publicclassTest2InterceptorimplementsWebRequestInterceptor{ @Override publicvoidpreHandle(WebRequestwebRequest)throwsException{ } @Override publicvoidpostHandle(WebRequestwebRequest,ModelMapmodelMap)throwsException{ } @Override publicvoidafterCompletion(WebRequestwebRequest,Exceptione)throwsException{ } }
过滤器:依赖于servlet容器,使用回调函数,过滤范围大
拦截器:依赖于框架容器比如spring、mybatis,灵活
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持毛票票。