The role of interceptor
Spring MVC's interceptor is similar to the filter in Servlet development, and is used for preprocessing and postprocessing of the processor. Interceptors are connected into a chain in a certain order. This chain is called Interceptor Chain. When an intercepted method or field is accessed, the interceptors in the interceptor chain will be called in the order they were previously defined. Interceptors are also the specific implementation of AOP ideas.
The difference between interceptor and filter
The difference | Filter | Intercepter |
Scope of use | is part of the servlet specification and can be used by any Java Web project | is the SpringMVC framework's own, only if SpringMVC is used Only framework projects can use |
Interception range | After configuring /* in the url-pattern, can intercept all resources to be accessed | After configuring /** in |
Interceptor Quick Start
Customized interception steps:
① Create an interceptor class to implement the HandlerInterceptor interface
public class MyHandlerInterceptor1 implements HandlerInterceptor { public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) { System.out.println("preHandle running..."); return true; } public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) { System.out.println("postHandle running..."); } public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) { System.out.println("afterCompletion running..."); } }
② Configure the interceptor
<!--配置拦截器--> <interceptors> <interceptor> <mapping></mapping> <bean></bean> </interceptor> </interceptors>
③ Test the interception effect of the interceptor (write the target method)
@RequestMapping("/quick23") @ResponseBody public ModelAndView quickMethod23() throws IOException, ParseException { System.out.println("目标方法执行...."); ModelAndView modelAndView = new ModelAndView(); modelAndView.addObject("name","modelname"); modelAndView.setViewName("index"); return modelAndView; }
(Access URL)
http ://localhost:8080/project/quick23
Console printing results
Multiple interceptor operations
The steps are the same as above , before writing a MyHandlerInterceptor2 operation, just test the execution sequence.
Interceptor method description
Description | |
The method will be called before request processing. The return value of this method is of Boolean type. When it returns false, it means that the request is over. Subsequent Interceptor and Controller will not be executed again; when it returns When the value is true, the preHandle method of the next Interceptor will continue to be called. | |
This method is called after the current request is processed, provided that The return value of the preHandle method can only be called when it is true, and it will be called before the DispatcherServlet returns the view for rendering, so we can operate the ModelAndView object after the Controller is processed in this method | |
This method will be executed after the entire request is completed, that is, after DispatcherServlet renders the corresponding view. The premise is that the return value of the preHandle method is true before it can be called. |
The above is the detailed content of How to use Java's SpringMVC interceptor. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Safe Exam Browser
Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

WebStorm Mac version
Useful JavaScript development tools

SAP NetWeaver Server Adapter for Eclipse
Integrate Eclipse with SAP NetWeaver application server.

MinGW - Minimalist GNU for Windows
This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

Atom editor mac version download
The most popular open source editor