Interceptor: Used to intercept the URL of the access
Use: Permission validation, garbled settings, etc.
Configuration in the Spring-mvc.xml file:
<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/sprin G-beans-3.1.xsd Http://www.springframework.org/schema/context HTTP://WWW.SPR Ingframework.org/schema/context/spring-context-3.1.xsd HTTP://WWW.SPRINGFRAMEWORK.ORG/SCHEMA/MV C http://www.springframework.org/schema/mvc/spring-mvc-3.1.xsd http://www.springframe Work.org/schema/tx " >
<!--Writing interceptors - <mvc:interceptors> <!--interception of specific URLs - <Mvc:interceptor> <mvc:mappingPath= "/test.do"/> <Beanclass= "Com.hbut.interceptor.TestInterceptor"/> </Mvc:interceptor> <Mvc:interceptor> <!--interception of specific modules - <mvc:mappingPath= "/test"/> <Beanclass= "Com.hbut.interceptor.TestInterceptor"/> </Mvc:interceptor> </mvc:interceptors>
Interception of all URLs
< mvc:interceptors > < class= "Org.springframework.web.servlet.i18n.LocaleChangeInterceptor"/> </mvc:interceptors>
Java code
PackageCom.hbut.interceptor;ImportOrg.springframework.web.servlet.HandlerInterceptor;ImportOrg.springframework.web.servlet.ModelAndView;Importjavax.servlet.http.HttpServletRequest;ImportJavax.servlet.http.HttpServletResponse;/*** @Author Xijun.gong * @DATE 2016/6/1. * Aim:com.hbut.interceptor*/ Public classTestinterceptorImplementsHandlerinterceptor {@Override Public BooleanPrehandle (HttpServletRequest httpservletrequest, HttpServletResponse httpservletresponse, Object o)throwsException {
Todo add here to manipulate the code
System.out.println ( "Prehandle"); return true; When Todo here is false, the request does not reach the control layer} @Override Public voidPosthandle (HttpServletRequest httpservletrequest, HttpServletResponse httpservletresponse, Object o, ModelAndView Modelandview)throwsException {System.out.println ("Posthandle"); Todo can be used to modify information, jump, etc.} @Override Public voidAftercompletion (HttpServletRequest httpservletrequest, HttpServletResponse httpservletresponse, Object o, Exception E )throwsException {System.out.println ("Aftercompletion"); Todo Last execution}}
Another type of interceptor: Similar
PackageCom.hbut.interceptor;ImportOrg.springframework.ui.ModelMap;Importorg.springframework.web.context.request.WebRequest;ImportOrg.springframework.web.context.request.WebRequestInterceptor;/*** @Author Xijun.gong * @DATE 2016/6/1. * Aim:com.hbut.interceptor*/ Public classTest2interceptorImplementsWebrequestinterceptor {@Override Public voidPrehandle (WebRequest WebRequest)throwsException {} @Override Public voidPosthandle (WebRequest WebRequest, Modelmap modelmap)throwsException {} @Override Public voidAftercompletion (WebRequest WebRequest, Exception e)throwsException {}}
Filter: Dependent on servlet container, using callback function, filtering range is large
Interceptors: Rely on framework containers such as spring, mybatis, flexible
Reference article: 1. Http://blog.sina.com.cn/s/blog_6a0cd5e501012bt9.html
2. http://haohaoxuexi.iteye.com/blog/1750680
SPRINGMVC Interceptor and Filter Summary