Spring MVC Interceptor

Source: Internet
Author: User

Spring provides us with:

Org.springframework.web.servlet. Handlerinterceptor Interface,

Org.springframework.web.servlet.handler. Handlerinterceptoradapter Adapter,

Implement this interface or inherit this class, it is very convenient to implement their own interceptors.

For example:

 Public classHelloworldinterceptorImplementsHandlerinterceptor {@Override Public BooleanPrehandle (httpservletrequest request, httpservletresponse response, Object handler)throwsException {System.out.println ("Pre-handle"); return false; } @Override Public voidPosthandle (httpservletrequest request, httpservletresponse response, Object handler, Modelandview m Odelandview)throwsException {System.out.println ("Post-handle"); } @Override Public voidaftercompletion (httpservletrequest request, httpservletresponse response, Object handler, Exception ex) throwsException {System.out.println ("After completion handle"); }  

There are three ways to do this:

Action before executing:

public boolean prehandle (HttpServletRequest request, httpservletresponse response, Object handler);

Execute before generating view

public void Posthandle (HttpServletRequest request, httpservletresponse response, Object handler, Modelandview Modelandview);

Last execution, can be used to release resources

public void Aftercompletion (httpservletrequest request,httpservletresponse response, Object handler, Exception ex)

preprocessing, post processing (call service and return Modelandview, but no page rendering), return processing (page already rendered)

In the Prehandle, the coding, security control and so on can be processed;

In Posthandle, there is an opportunity to modify the Modelandview;

In aftercompletion, logging is possible based on whether an ex is null to determine if an exception has occurred.

The object handler in the parameter is the next interceptor.

There are three ways to configure the spring MVC configuration file:

Scenario one, (approximate) total Interceptor, block all URLs

In the case of restful URLs, static resources are also intercepted.

< mvc:interceptors >      <  class= "Com.app.mvc.MyInteceptor"/>  </MVC: Interceptors>  

Scenario two, intercept the matching URLs.

In the case of restful URLs, static resources are also intercepted.

<mvc:interceptors>      <Mvc:interceptor>            <mvc:mappingPath= "/user/*" /> <!--/user/* -            <Beanclass= "Com.mvc.MyInteceptor"></Bean>        </Mvc:interceptor>    </mvc:interceptors>    

Scenario three, the interceptor on the Handlermappint.

<Beanclass= "Org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping">        < Propertyname= "Interceptors">            <List>                <Beanclass= "Com.mvc.MyInteceptor"></Bean>           </List>        </ Property>       </Bean>  

If <mvc:annotation-driven/> is used, it automatically registers defaultannotationhandlermapping with Annotationmethodhandleradapter These two beans, so there is no chance to inject it with the interceptors attribute, you cannot specify an interceptor.

We can inject the interceptors attribute into the interceptor by manually configuring the above two beans without using <mvc:annotation-driven/>.

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.