Springboot项目搭建(三)整合thymeleaf模板

springboot整合thymeleaf模板

一、POM文件添加依赖

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

<!--nekohtml 解决thymealeaf标签闭合问题-->
<dependency>
    <groupId>net.sourceforge.nekohtml</groupId>
    <artifactId>nekohtml</artifactId>
    <version>1.9.14</version>
</dependency>

二、创建项目结构

|- src
  |- main
    |- resources
      |- templates
      

三、yml配置文件(properties文件同下)

常用属性
spring.thymeleaf.cache        是否开启模板缓存,默认true
spring.thymeleaf.encoding     指定模板的编码,默认为: UTF-8
spring.thymeleaf.prefix       指定模板的前缀,默认为:classpath:/templates/
spring.thymeleaf.suffix       指定模板的后缀,默认为:.html
spring.thymealeaf.mode        指定模板的模式, 默认为:HTML5 (如果使用了nekohtml依赖 设置为LEGACYHTML5)

参考 xixicat SpringBoot配置属性之MVC

四、演示页面

路径:src.main.resoruces.templates
demo.html

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8"> <!-- 未导入nekohtml依赖且修改mode时,此处需闭合,否则会报错 -->
    <title>Thymealeaf Page</title>
</head>
<body>
    <h1>Hello Thymealeaf!</h1>
</body>
</html>

五、编写controller

路径:src.main.java.域名反写.项目名.controller

@Controller
public class IndexController {

    @RequestMapping("demo")
    public String demo(){
        return "demo";
    }
}

六、启动项目并访问页面

《Springboot项目搭建(三)整合thymeleaf模板》

参考 纯洁的微笑 springboot(四):thymeleaf使用详解

    原文作者:roylion
    原文地址: https://segmentfault.com/a/1190000011907375
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞