Day 6 of spring MVC study (November 21, 2021)

Day 6 of spring MVC study (November 21, 2021)

Learn the interceptors of spring MVC:

1. Interceptors are used to intercept the execution of controller methods.
2. When will the three abstract methods in the interceptor be executed?
One before the controller method is executed
One after the controller method is executed
After a view is rendered.

How to create spring MVC interceptors?

Implementation interface: HandlerInterceptor

Override three methods:

package com.rtl.mvc.interceptor;

import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class FirstInterceptor implements HandlerInterceptor {
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        return HandlerInterceptor.super.preHandle(request, response, handler);

    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {
        HandlerInterceptor.super.postHandle(request, response, handler, modelAndView);

    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {
        HandlerInterceptor.super.afterCompletion(request, response, handler, ex);

Handle the garbled code in response Chinese:
Add this to the spring MVC configuration file:

//Before, it was just a simple annotation driver, but now it is configured with a lot of things.
            <bean class="org.springframework.http.converter.ObjectToStringHttpMessageConverter">
                <property name="defaultCharset" value="UTF-8"></property>
                <property name="supportedMediaTypes">

2. Configure the configuration file of spring mvc.xml to configure the interceptor

        <bean class="com.rtl.mvc.interceptor.FirstInterceptor"></bean>


The preHandle method is the interception method.
If this method returns false, it is intercepted
We set the method preHandle in the interceptor
Set his return value to false. That is, it will intercept.

At this time, we start the project,
Browser input http://localhost:8080/springMVC/
The home page will not be accessed by default, but the controller method will not be executed at this time because it is intercepted by the interceptor.

Only preHandle will be executed.
So only print:

If you change the return value of the preHandle method to true. It's release.

The return value of the method of preHandle is boolean. Whether to call the following controller method. true: call the controller method, that is, do not intercept. Release.

When the return value of the method is set to true, it is released.
Then all three methods will be executed. And the controller method will also execute, so you can visit the home page.

All requests are intercepted by default.

We need to selectively intercept some request paths.

            <mvc:mapping path="/testResponseBodyEmployee"/>
            <mvc:exclude-mapping path="/testResponseBody"/>
            <bean class="com.rtl.mvc.interceptor.FirstInterceptor"></bean>

If multiple interceptors are configured and all return true, the execution order of interceptor methods:
If it is in the spring MVC configuration file
FirstInterceptor is set first
Setting after SecondInterceptor
Then the execution order is:
First -> pre
Second -> pre

Second -> post
First -> post

Second -> after
First -> after

If there are two interceptors, the configuration is first in front and returns true
second is followed by false

For the pre method, it will be executed in the order of first second
For the post method, it is not executed
For the after method, only first executes

Learn here to simply learn the knowledge points of spring MVC.

Tags: Spring MVC

Posted on Sun, 21 Nov 2021 04:52:44 -0500 by Pantho