search
HomeJavajavaTutorialHow to use Java's SpringMVC interceptor

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 , you can also intercept all resources, but you can exclude them by passing the tag Resources that do not need to be intercepted

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

How to use Javas SpringMVC interceptor

Multiple interceptor operations

The steps are the same as above , before writing a MyHandlerInterceptor2 operation, just test the execution sequence.

Interceptor method description

##Method nameDescriptionpreHandle()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. postHandle()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 methodafterCompletion()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!

Statement
This article is reproduced at:亿速云. If there is any infringement, please contact admin@php.cn delete

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
1 months agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
1 months agoBy尊渡假赌尊渡假赌尊渡假赌
Will R.E.P.O. Have Crossplay?
1 months agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Safe Exam Browser

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

WebStorm Mac version

Useful JavaScript development tools

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Integrate Eclipse with SAP NetWeaver application server.

MinGW - Minimalist GNU for Windows

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

Atom editor mac version download

The most popular open source editor