Day 7 of SpringBoot self-study - thymeleaf + interceptor writes web page login function

Posted by mars16 on Mon, 27 Dec 2021 21:43:24 +0100

Previous chapter

Day 6 of SpringBoot self-study - anyone who dies and climbs the template home's member paid boutique website template

preface

Some problems in the video were barely solved, but the progress was only six videos. The @ Slf4j annotation in lombok is quite useful. It is recommended to use it.
Raytheon's web page (there is a link in yesterday's article) has always been the most thoughtless step-by-step.

1, Modify the web page to thymeleaf format

Put the project link at the end and directly compare it with yourself. The process is shown in Raytheon video.

1. Public page writing

The reference in the head tag is placed in the public page head, and the link is changed to th:href or th:src, a background injected template engine format.
During block reference, you can add div block elements to encapsulate, and then add id attribute or th:fragment attribute to Div

2. Reference method

There are three reference methods: th:insert, th:include and th:replace. Generally, replace is enough

difference:
th:include: load the content of the template: read the content of the loading node (excluding the node name) and replace the div content
th:replace: replace the current tag with the tag in the template. The loaded node will replace the loaded div
th:insert: inserts the template content directly into the current tag

Call method:

  1. If you are in the same directory, the name of the public page is: common HTML. If the referenced fragment name is foot, the reference method is: < div th: replace = "common:: foot" / >
  2. If the public page is named common in the common directory HTML, the reference method is: < div th: replace = "common / common:: foot" / >
  3. If the frame attribute to be referenced is under the current page, you can directly obtain the public fragment by omitting the page name: < div th: replace = ":: foot" / >
  4. If you want to load the whole page instead of a fragment, you can write the page relative path / file name directly, for example: < div th: replace = "common" / > or < div th: replace = "common / common" / >
  5. If the referenced fragment has no fragment attribute but only ID, you can use: < div th: replace = "common:: #id attribute" / > (similar to the writing method with fragment, just add a pound sign in front of the attribute (after the double colon)

3. th:each advanced usage

<!--Basic usage-->
<tbody>
    <tr th:each="user:${users}">
        <td th:text="${user.username}"></td>
        <td th:text="${user.password}"></td>
    </tr>
</tbody>

2, Interceptor usage steps

1. Create an interceptor class that inherits from HandlerInterceptor

After inheriting the interceptor class, you can override the three functions inside:

@Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
	//do something to do before interception
    return false;
}

@Override
public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {
//do something intercepts things that pass. Here, you can change the request parameters and return values of the web page before the request reaches the response function of the response
}

@Override
public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {
    //I don't know what to do after the request is answered, and I forget my previous knowledge
}

2. Modify the MyConfig configuration and add an interceptor

The code is as follows (example):

//The addPathPatterns function is the path to add interceptions
//The excludePathPatterns function discards intercepted paths (non intercepted paths)
@Configuration
public class MyConfig implements WebMvcConfigurer {

    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(new LoginIntercepter())
                .addPathPatterns("/**")
                .excludePathPatterns("/","/login","/login.html","/css/**","/fonts/**","/images/**","/js/**","/error/**","/favicon.ico");
    }
}

The business logic of the login function is not written here. It is relatively simple, not to mention the integration of the database.

summary

It's boring to modify web pages, but it's more interesting to write business logic.
I initialize the springboot project template through IDEA. The request always times out. Alas, I can only download the template on the web page again.

gitee project address: Day 7 of SpringBoot self-study - thymeleaf + interceptor writes web page login function

If an error is reported, please check the lombok plug-in, delete the item cache, reload the index, and clear the cache: File - > invalidate - > check the first option - > confirm.

Topics: Java Maven Spring Boot IDEA