The main content of this chapter:
Through the previous study, we understood and quickly completed the first application of spring boot. Spring boot enterprise-level framework, how does spring boot read static resources? What about js folders, css files and png/jpg images? How to customize the message converter? How to customize the configuration of spring mvc? These are what we need in the company. How to solve these? These are explained in detail in the following subsections. Well, now open the fourth section of spring boot's web development
This section mainly:
1: Custom message converter
This article is the eighth spring boot framework learning 8-spring boot web development (4) - custom spring mvc configuration
Disclaimer: This article is original by Kaige Java (www.kaigejava.com), reprinting is prohibited without permission!
Main content of this section:
1: Learn to customize the configuration of spring mvc by adding interceptors
2: Detailed explanation of the three methods of the HandlerInterceptor object
One: Learn to customize the configuration of spring mvc by adding interceptors
1.1: Requirements Description
Sometimes, we don't need to configure spring mvc ourselves instead of using springt boot
Default approval. For example, in the background, users must log in to access the corresponding page. At this time, we need to add an interceptor to deal with it. How to deal with this situation?
1.2: How to add an interceptor
Interceptors need to be added. At this time, they must be extended by inheriting webmvcconfigureadapter and then rewriting the methods in the parent class.
As shown below:
View the running results:
User is not logged in:
Take a screenshot after adding user user=123 to the address:
Two: Detailed explanation of the three methods of the HandlerInterceptor object
2.1:preHandle(HttpServletRequest request,HttpServletResponse response, Object handler)
From the above picture, the user has already logged in, but it can be seen from the red selected part of the screenshot above, that the preHandle method will be called before executing the controller processing. The source code is as follows:
The general meaning is that when true is returned, the chain behind it is executed. If it returns false, the request is terminated directly.
The following picture is my personal understanding:
So, if we want permission handling:
2.2:postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView)方法:
Look at the source code first:
This is when the post request is, and the method is executed. It will not be explained in detail here.
2.3:afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex)。
understand:
This section focuses on custom interceptors to handle some requests.
In the next section, we will explain how to jump to the jsp page and return the 404 error code and the function of the current browsing page that continues to jump after logging in. Everyone is welcome to continue learning.
Other articles in this series:
Spring boot framework learning 1 - know spring boot and quick start
Spring boot framework learning 2-spring boot core (1)
Spring boot framework learning 3-spring boot core (2)
Spring boot framework learning 5-spring boot web development (1)
Spring boot framework learning 6-spring boot web development (2)
The spring boot framework learns 7-spring boot web development (3) - custom message converter |
Welcome to the public account of Kaige: Kaige Java
Welcome to Kaige's personal website: www.kaigejava.com
Source of this article: http://www.kaigejava.com/article/detail/5 7
Spring boot series tutorial address: http://kaigejava.com/article/list?cateid=3