Spring MVC Definition Interceptor

Source: Internet
Author: User
Tags object object

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.