SPRINGMVC's Interceptor

Source: Internet
Author: User
Tags stub

Springmvc The Interceptor Interceptor is also quite important and useful, and its main function is to intercept the user's request and handle it accordingly. For example, by using it to verify permissions, or to determine whether the user is logged in

The steps are as follows:

1. First define an interceptor class and implement the Handlerinterceptor interface as follows:

public class Logininterceptor implements Handlerinterceptor {

/**

* Prehandle method is used for processor interception, as the name implies, the method will be called before the controller processing, SPRINGMVC in the Interceptor interceptor is chained, can exist simultaneously

* Multiple Interceptor, then SPRINGMVC will execute one after the other according to the order of the Declaration, and all Prehandle methods in interceptor will be

* Called before the Controller method call. This kind of interceptor chain structure of SPRINGMVC can also be interrupted, this kind of interrupt mode

is to make Prehandle's return

* Return value is false, when the return value of Prehandle is false, the entire request ends.

*/

@Override

public boolean prehandle (HttpServletRequest request,

HttpServletResponse response, Object handler) throws Exception {

System.out.println ("******* 1th Interceptor Interception 111*********");

return true;

}

/**

* This method will only be executed if the current interceptor Prehandle method returns a value of true. Posthandle is used for processor interception, its execution time is in the processor processing

*, which is executed after the controller's method call, but it executes before the Dispatcherservlet renders the view, that is, in this method you can perform a Modelandview

For The chain structure of this method is the opposite of the normal access direction, that is to say the first declaration of the Interceptor Interceptor this method will be called later, which is a bit like the execution process of the interceptor inside the STRUTS2,

* Just Struts2 inside the Intercept method to call the Actioninvocation invoke method manually, the Invoke method called Actioninvocation in Struts2 is called the next Interceptor

* Either call the action and then the content to be called before Interceptor is written before invoking invoke, and the content to be called after Interceptor is written after the Invoke method is called.

*/

@Override

public void Posthandle (HttpServletRequest request,

HttpServletResponse response, Object handler,

Modelandview Modelandview) throws Exception {

TODO auto-generated Method Stub


}

/**

* This method is also required when the return value of the Prehandle method of the current corresponding interceptor is true before execution. The method will render the view execution after the entire request is completed, that is, Dispatcherservlet.

* The main function of this method is to clean up resources, of course, this method can only be executed when the return value of the current interceptor Prehandle method is true.

*/

@Override

public void Aftercompletion (HttpServletRequest request,

HttpServletResponse response, Object handler, Exception ex)

Throws Exception {

TODO auto-generated Method Stub


}


}

2. Next, add the following configuration entry in the spring applicationcontext.xml configuration file as follows:

<mvc:interceptors>

<!--the interceptor defined in the root directory will intercept all requests--

<bean class= "Com.tsh.interceptor.AllInterceptor"/>

<!--the representation below Mvc:interceptor is intercepted for a specific request and can be intercepted multiple times --

<mvc:interceptor>

<mvc:mapping path= "/serverdegrade/getallserver.do"/>

<bean class= "Com.tsh.interceptor.LoginInterceptor"/>

</mvc:interceptor>

<mvc:interceptor>

<mvc:mapping path= "/serverdegrade/getallserver.do"/>

<bean class= "Com.tsh.interceptor.ServerInterceptor"/>

</mvc:interceptor>

</mvc:interceptors>


Reference link url:http://elim.iteye.com/blog/1750680

This article is from the "flyfish" blog, make sure to keep this source http://9381188.blog.51cto.com/9371188/1874596

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.