SpringBoot整合thymeleaf

2020-03-08  本文已影响0人  CodePandaes

thymeleaf简介

Spring Boot提供了多种模板引擎的默认配置支持,但嵌入式容器JSP有限制,2010年后Velocity停止更新,所以这JSP与Velocity两个不建议使用。而Thymeleaf与SpringMVC的视图技术,及SpringBoot的自动化配置集成非常完美,几乎没有任何成本,你只用关注Thymeleaf的语法即可。

Thymeleaf的特点

和SpringBoot整合

  1. 新建SpringBoot项目(可参考我的上一篇博客SpringBoot初体验),打开pom.xml,添加thymeleaf依赖
<dependencies>
        <!--web容器-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <!--前端模板thymeleaf依赖-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-thymeleaf</artifactId>
        </dependency>
</dependencies>
  1. 打开配置文件application,yml(application.properties),关闭thymeleaf缓存
  1. 新建HelloWorldController类,返回hello页面
  1. templates目录下新建hello.html

5.打开SpringBootThymeleafApplication类启动项目

  1. 浏览器中输入localhost:8080/hello,访问成功
上一篇 下一篇

猜你喜欢

热点阅读