user3541321
user3541321

Reputation: 165

Spring mvc interceptor Basics

I am new to Spring and currently working on a spring application. I am very confused regarding the spring interceptor and Interceptor in Spring security.

I have below doubts.
1. What is the use of Interceptor ? Is it used to modify the requested url or to validate the url ?
2. Is it possible that through interceptor i can modify my url /Test/MyTest to /Test/Intercept/MyTest ?
3. If Interceptor is only used to vaidate the url then only by url-pattern=/"somevalue" it will work or need to implement Interceptorhandler ?

Please help me out to understand these basic functionalities of interceptor.

I went through lot of sites but still not clear about all these concepts.

Upvotes: 0

Views: 850

Answers (3)

Moein Almasi
Moein Almasi

Reputation: 131

Spring MVC Interceptor is similar to Servlet Fiter Concept. Spring MVC provides ability to define set of classes called interceptor which will be called before and after a request being served. Interceptor will implement HandlerInterceptor which following thee methods need to be implemented:

  1. preHandle() : Called before the handler execution
  2. postHandle() : Called after the handler execution
  3. afterCompletion() : Called after the complete request has finished

There is a good tutorial by MKYONG which I recommend you to have a look at it that I found it helpful in understanding the fundamental concept of interceptor. Hope that helps to get you started.

Upvotes: 0

Serge Ballesta
Serge Ballesta

Reputation: 148900

An interceptor is somewhat like a filter. A filter processes the request and response around a servlet and an interceptor processes the request and optionally the model around a spring controller. Common uses are pre-processing a request to ensure that a condition is realized (preHandle), or populating the model with attributes common to different controller methods (postHandle). afterCompletion is mainly used to perform cleanup at the end of request processing.

Upvotes: 1

Mayank
Mayank

Reputation: 61

     Actually interceptor can do three things

            preHandle(…) – called just before the action
            postHandle(…) – called immediately after the action
            afterCompletion(…) – called just before sending response to view

        Best example of prehandle is-checking whether the user is logged in or not.


      Hope you have got some idea of interceptor

Upvotes: 0

Related Questions