spring boot2.3.0集成 thymelaf

 

  1. 配置pom

    1. 如果是2.x的直接配置一个starter即可

        <!-- ThymeLeaf 依赖 -->
      <dependency>
       <groupId>org.springframework.boot</groupId>
       <artifactId>spring-boot-starter-thymeleaf</artifactId>
      </dependency>

       

    2. 如果是1.x的 还需要配置一些参数

        <!-- 布局功能的支持程序  thymeleaf3主程序  layout2以上版本 -->
      <thymeleaf.version>3.0.9.RELEASE</thymeleaf.version>
      <thymeleaf-layout-dialect.version>2.2.2</thymeleaf-layout-dialect.version>

       

  2. resoruces下面建立 templates文件夹

  3. 把html文件放在templates里面,以.html结尾,可以在html里面加入命名声明

    1. <html lang="en" xmlns:th="http://www.thymeleaf.org">

       

  4. 编写controller类,用@Controller 不要用@RestController!如果前后端分离不是很彻底的项目,建议在controllr包 分为API和view包。分别放纯接口返回一个放模板访问的

    @Controller //注意  不要用 RestController
    public class Say {
        @RequestMapping("hi") 
        public String hi() {
            return "/list"; //需要加入”/“, 反而RequestMapping里面如果是直接访问反而不需要"/”
        }
    }
    

      

  5. 配置application.yml文件(非强制)
    spring:
        thymeleaf:
            mode: HTML
            encoding: UTF-8
            servlet:
                content-type: text/html
            prefix: classpath:/templates
            suffix: .html
            cache: false
    

      

     

     

     

原文地址:https://www.cnblogs.com/jinbiao/p/13048308.html