SPRINGMVC's Interceptor

Source: Internet
Author: User

There are two ways to configure spring interceptors

1. Implement Interface: Handleinterceptor

public class MyInterceptor1 implements Handlerinterceptor {@Overridepublic void aftercompletion (httpservletrequest Request, httpservletresponse response, Object obj, Exception e) throws Exception {System.out.println ("last run.!! Typically used to release resources. ");} @Overridepublic void Posthandle (HttpServletRequest request, httpservletresponse response, Object obj, Modelandview Model) throws Exception {System.out.println ("After the action is run, run before build view!! ");} @Overridepublic boolean prehandle (httpservletrequest request, httpservletresponse response, Object obj) throws Exception {System.out.println ("Run before" Action!

! "); return true; Continue to run the action}}

A) Prehandle

This method runs before the action is run. It is possible to preprocess the data. For example: coding, security control, and so on.

Assuming the method returns true, the action continues to run.

b) Posthandle

This method runs after the action is run, before the view is generated. Over here. We have the opportunity to change the view layer data.

c) aftercompletion

Last run. Often used to dispose of resources and handle exceptions.

We are able to do the related exception handling based on whether the ex is empty.

Since we normally handle exceptions, we throw exceptions from the bottom up. Finally, the spring framework has come to this approach.


2. Inheritance adapter: Handleinterceptoradapter

public class MyInterceptor2 extends Handlerinterceptoradapter {@Overridepublic Boolean prehandle (httpservletrequest Request, HttpServletResponse response, Object handler) throws Exception {System.out.println ("Myinterceptor2.prehandle () "); return true; Continue to run the action}}

3. Configure User-servlet.xml

Scenario 1:

<!--Configure yourself to define the Interceptor--><mvc:interceptors><bean class= "Com.zdp.interceptor.MyInterceptor1" ></bean > <!--intercept all url!

--></mvc:interceptors>

Scenario 2:

<!--Configure yourself to define the Interceptor--><mvc:interceptors><mvc:interceptor><mvc:mapping path= "/user/add"/> <! --just intercept join user--><bean class= "Com.zdp.interceptor.MyInterceptor2" ></bean></mvc:interceptor> </mvc:interceptors>






SPRINGMVC's 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.