SpringBoot整合Thymeleaf使用

SpringBoot整合Thymeleaf使用

使用感想:针对企业官网、个人博客等使用最佳,大型项目还是老实前后端分离吧。

1、什么是Thymeleaf

Thymeleaf Web 和独立环境的现代服务器端 Java 模板引擎,Spring Boot 官方推荐使用  thymeleaf 而不是 JSPSpring Boot 已经提供了默认的配置,比如解析的文件前缀,文件后缀,文件编码,缓存等等

2、使用方法

a、pom依赖 

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

b、添加html标签 

<html lang="zh-CN" xmlns:th="http://www.thymeleaf.org">

c、配置视图解析器 

spring-boot很多配置都有默认配置,比如默认页面映射路径为 classpath:/templates/*.html 
同样静态文件路径为 classpath:/static/ 

application.properties配置 

spring.thymeleaf.mode=HTML5 
spring.thymeleaf.encoding=UTF-8 
spring.thymeleaf.content-type=text/html 
spring.thymeleaf.cache=false  #开发时关闭缓存,不然没法看到实时页面  

d、常用标签

th:text=”${}” th:utextth:if th:eachth:value

th:src=@{}”、 th:href =@{}等可参考官网

 

示例:

接口返回:

model.addAttribute("acticeId", "a1");

html页面接收:

<span id="activeId" th:text="${activeId}" style="display: none"></span>

 

posted on 2020-05-06 11:39  fuanfei  阅读(230)  评论(0)    收藏  举报