springboot (Thymeleaf, interceptors)

A common template engine

JSP、Velocity、Freemarker、Thymeleaf

1.1.Thymeleaf entry

The principle template engine
template engine can be seen as a file (content) synthesis technology, we can use some template engine (Freemarker), use the fill data, the template can merge output file responses, such as: model (data) + ftl (template file) = html (merged into html)

1.1.1. Jar package introduced

<!--thymeleaf模板-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-thymeleaf</artifactId>
        </dependency>
        
<!--如果用到jquery 导入jquery jar包-->
        <dependency>
            <groupId>org.webjars</groupId>
            <artifactId>jquery</artifactId>
            <version>3.4.1</version>
        </dependency>


As long as we put HTML pages on the classpath: / templates /, thymeleaf will automatically rendered;

1.1.2 Create Create File: resources / templates / hello.html

<!DOCTYPE html>
<!--导入命名空间 -->
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>


<script src="webjars/jquery/3.4.1/jquery.js">
</script>
<script>
    alert($)
</script>


<body>
<h1>成功!</h1>
<!--使用语法th:text 将div里面的文本内容设置为 -->
<div th:text="${hello}">这是显示欢迎信息</div>
</body>
</html>

1.1.3. Writing controller

package cn.itcource.web.controller;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

@Controller
public class HelloController {
    @RequestMapping("/hello")
    public String hello(Model model){
        model.addAttribute("hello","王哈哈");
        return "hello";
    }
}

Two, Thymeleaf automatic configuration principle

1.Thymeleaf arrangement

  • Thymeleaf can do to adjust the configuration, but is not required, it is recommended not to configure the default configuration suffice.
spring:
  thymeleaf:
    enabled: true
    cache: false
    encoding: utf-8
    suffix: .html
    prefix:
    servlet:
      content-type: text/html

2.Thymeleaf automatic configuration principle

  • CI is read into ThymeleafProperties object,
  • ThymeleafAutoConfiguration it will use the configuration of the project ThymeleafProperties, automatic configuration Thymeleaf

Here Insert Picture Description
Click to jump to any configuration item
Here Insert Picture Description
ThymeleafAutoConfiguration it will use the configuration of the project ThymeleafProperties

Then automatically configures Thymeleaf
Here Insert Picture Description

Third, the configuration of the interceptor

Define interceptor class

/**
 * 拦截器(可以继承HandlerInterceptorAdapter)
 * 也可以实现
 *
 *      可以打上注解@Configuration交给spring管理,然后WebConfig就不用new,直接注入
 *
 */
@Slf4j
public class MyInterceptor implements HandlerInterceptor {

    //在controller方法调用前,先执行该方法,
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        String requestURI = request.getRequestURI();
        log.info("拦截... uri:" + requestURI);
        return true;
    }
}

Registration interceptor (interceptor created with the class: config / WebConfig)

/**
 * 针对web的配置
 *      WebMvcConfigurer:针对于springmvc配置的接口
 */

@Configuration
public class WebConfig implements WebMvcConfigurer {

    //添加拦截器的方法
    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        //添加拦截器
        registry.addInterceptor(new MyInterceptor())
                //拦截所有请求
                .addPathPatterns("/**")
                //排除请求、资源
                .excludePathPatterns("/static/**","/hello","/webjars/**","/login");
    }

    //添加视图控制器(访问/wxy这个请求路径、跳转到resources下的templates/hello2.html)
    @Override
    public void addViewControllers(ViewControllerRegistry registry) {
        registry.addViewController("/wxy").setViewName("hello2.html");
    }
}

Published 33 original articles · won praise 0 · Views 401

Guess you like

Origin blog.csdn.net/weixin_45737653/article/details/104928077