Home > Java > javaTutorial > How to use Java's SpringMVC interceptor

How to use Java's SpringMVC interceptor

WBOY
Release: 2023-05-13 14:55:06
forward
1402 people have browsed it

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...");
    } 
}
Copy after login

② Configure the interceptor

<!--配置拦截器--> 
<interceptors>
 <interceptor>
  <mapping></mapping>
  <bean></bean>
 </interceptor>
</interceptors>
Copy after login

③ 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;
}
Copy after login

(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!

Related labels:
source:yisu.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template