Interceptors:
PackageSy. Interceptor;Importjavax.servlet.http.HttpServletRequest;ImportJavax.servlet.http.HttpServletResponse;ImportOrg.springframework.web.servlet.HandlerInterceptor;ImportOrg.springframework.web.servlet.ModelAndView; Public classMyinterceptorImplementsHandlerinterceptor { Public BooleanPrehandle (HttpServletRequest request, HttpServletResponse response, Object object)throwsException {System.out.println ("Prehandle"); return true; } Public voidPosthandle (HttpServletRequest request, HttpServletResponse response, Object object, Modelandview Modelandview)throwsException {System.out.println ("Posthandle"); } Public voidAftercompletion (HttpServletRequest request, HttpServletResponse response, Object object, Exception Exception)throwsException {System.out.println ("Aftercompletion"); }}
To configure the interceptor in the Spring MVC configuration file:
<?XML version= "1.0" encoding= "UTF-8"?><Beansxmlns= "Http://www.springframework.org/schema/beans"Xmlns:mvc= "Http://www.springframework.org/schema/mvc"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"xsi:schemalocation= "Http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/ Spring-beans-3.0.xsd Http://www.springframework.org/schema/context Http://www.springframework.org/schema/context /spring-context-3.0.xsd Http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/ Spring-mvc-3.0.xsd "> <!--automatically scans all classes under the controller package to make it think that spring MVC's controllers - <Context:component-scanBase-package= "Sy.controller" />
<!--configuring Interceptors--
<mvc:interceptors> <Mvc:interceptor> <!--The URL path is matched, and if not configured or/**, all controllers will be blocked - <mvc:mappingPath="/**" /> <Beanclass= "sy." Interceptor.myinterceptor "></Bean> </Mvc:interceptor> </mvc:interceptors> <!--JSON mapping solves Chinese garbled problem - <BeanID= "Mappingjacksonhttpmessageconverter"class= "Org.springframework.http.converter.json.MappingJackson2HttpMessageConverter"> < Propertyname= "Supportedmediatypes"> <List> <value>Text/html;charset=utf-8</value> </List> </ Property> </Bean> <!--start the Spring MVC Annotation feature to complete the mapping of requests and annotations Pojo - <Beanclass= "Org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter"> < Propertyname= "Messageconverters"> <List> <refBean= "Mappingjacksonhttpmessageconverter" /><!--JSON Converter - </List> </ Property> </Bean> <!--resolution of the Model view name, that is, adding a prefix to the Model view name - <Beanclass= "Org.springframework.web.servlet.view.InternalResourceViewResolver"P:prefix= "/web-inf/pages/"P:suffix= ". jsp" /> <BeanID= "Multipartresolver"class= "Org.springframework.web.multipart.commons.CommonsMultipartResolver"> < Propertyname= "Defaultencoding"> <value>UTF-8</value> </ Property> < Propertyname= "Maxuploadsize"> <value>32505856</value><!--upload file Size limited to 31m,31*1024*1024 - </ Property> < Propertyname= "Maxinmemorysize"> <value>4096</value> </ Property> </Bean></Beans>
Spring MVC Definition Interceptor