如何在SpringBoot中引入Thymeleaf

发布时间:2021-04-20 16:42:38 作者:Leah
来源:亿速云 阅读:263

如何在SpringBoot中引入Thymeleaf?针对这个问题,这篇文章详细介绍了相对应的分析和解答,希望可以帮助更多想解决这个问题的小伙伴找到更简单易行的方法。

springboot是什么

springboot一种全新的编程规范,其设计目的是用来简化新Spring应用的初始搭建以及开发过程,SpringBoot也是一个服务于框架的框架,服务范围是简化配置文件。

1.Thymeleaf简介

Thymeleaf是个XML/XHTML/HTML5模板引擎,可以用于Web与非Web应用 

Thymeleaf的主要目标在于提供一种可被浏览器正确显示的、格式良好的模板创建方式,因此也可以用作静态建模,Thymeleaf的可扩展性也非常棒。你可以使用它定义自己的模板属性集合,这样就可以计算自定义表达式并使用自定义逻辑,Thymeleaf还可以作为模板引擎框架。

2.引入Thymeleaf

引入依赖

在maven(pom.xml)中直接引入:

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

配置Thymeleaf

在application.yml配置Thymeleaf

server:
 port: 8000
spring:
 thymeleaf:
 cache: false # 关闭页面缓存
 encoding: UTF-8 # 模板编码
 prefix: classpath:/templates/ # 页面映射路径
 suffix: .html # 试图后的后缀
 mode: HTML5 # 模板模式

# 其他具体配置可参考org.springframework.boot.autoconfigure.thymeleaf.ThymeleafProperties
# 上面的配置实际上就是注入该类的属性值

demo示例

创建IndexController

@Controller
public class IndexController {
 // 返回视图页面
 @RequestMapping("index")
 public String index(){
  return "index";
 }

}

创建index.html

<!DOCTYPE html>
<html lang="en">
<head>
 <meta charset="UTF-8">
 <title>Title</title>
</head>
<body>
 Hello Thymeleaf!
</body>
</html>

创建TestController

@RestController
public class TestController {
 
 // 返回整个页面
 @RequestMapping("/test")
 public ModelAndView test(){
  return new ModelAndView("test");
 }
}

创建test.html

<!DOCTYPE html>
<html lang="en">
<head>
 <meta charset="UTF-8">
 <title>Title</title>
</head>
<body>
Hello Thymeleaf! </br>
By: ModelAndView
</body>
</html>

3.测试结果

如何在SpringBoot中引入Thymeleaf
如何在SpringBoot中引入Thymeleaf

4.Thymeleaf基础语法及使用

1.引入标签 

html标签里引入xmlns:th="http://www.thymeleaf.org"才能使用th:*这样的语法

2.引入URL 

@{...} 

例如:

<a th:href="@{http://www.baidu.com}" rel="external nofollow" >绝对路径</a> 是访问绝对路径下的URL, <a th:href="@{/}" rel="external nofollow" >相对路径</a> 是访问相对路径下的URL。

<a th:href="@{css/bootstrap.min.css}" rel="external nofollow" >是引入默认的static下的css文件夹下的bootstrap文件,类似的标签有: th:href 和 th:src

3.获取变量 

通过${}取值,对于JavaBean的话,使用变量名.属性名获取

4.字符串替换

<span th:text="'Welcome to our application, ' + ${user.name} + '!'"></span>
或者
<span th:text="|Welcome to our application, ${user.name}!|"></span>

注意:|…|中只能包含变量表达式${…},不能包含其他常量、条件表达式等

5.运算符 

   在表达式中可以使用各类算术运算符
   例如 (+, -, *, /, %)
   例如:th:with="isEven=(${stat.number} % 1 == 0)"
   逻辑运算符 (>, <, <=,>=,==,!=)
   需要注意的是使用<,>的时候需要转义

th:if="${stat.number} &gt; 1"
th:text="'Execution mode is ' + ( (${execMode} == 'dev')? 'Development' : 'Production')"

6.条件 

if/unless th:if是该标签在满足条件的时候才会显示,unless是不成立时候才显示

<a th:href="@{/login}" rel="external nofollow" th:unless=${user.number != null}>Login</a>

switch  thymeleaf支持switch结构,默认属性(default)用*表示

<div th:switch="${user.role}">
  <p th:case="'admin'">User is an administrator</p>
  <p th:case="#{roles.manager}">User is a manager</p>
  <p th:case="*">User is some other thing</p>
</div>

7.循环

<tr th:each="prod : ${prods}">
 <td th:text="${prod.name}">Onions</td>
 <td th:text="${prod.price}">2.41</td>
 <td th:text="${prod.inStock}? #{true} : #{false}">yes</td>
</tr>

8.Utilities

内置在Context中,可以直接通过#访问
#dates
#calendars
#numbers
#strings
arrays
lists
sets
maps

关于如何在SpringBoot中引入Thymeleaf问题的解答就分享到这里了,希望以上内容可以对大家有一定的帮助,如果你还有很多疑惑没有解开,可以关注亿速云行业资讯频道了解更多相关知识。

推荐阅读:
  1. SpringBoot怎么引入Thymeleaf方法
  2. 如何在SpringBoot中使用thymeleaf方法

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

springboot thymeleaf

上一篇:使用PHP怎么实现一个快递查询功能

下一篇:使用springboot怎么实现环境抽象

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》