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

springboot整合thymeleaf模板

1、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>

2、建立项目结构

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

3、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配置属性之MVChtml

4、演示页面

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

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

5、编写controller

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

@Controller
public class IndexController {

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

6、启动项目并访问页面

clipboard.png

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

相关文章
相关标签/搜索