SpringBoot引入Thymeleaf的方法介绍(代码)

互联网 19-4-4
本篇文章给大家带来的内容是关于SpringBoot引入Thymeleaf的方法介绍(代码),有一定的参考价值,有需要的朋友可以参考一下,希望对你有所帮助。

1、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、测试结果

4、Thymeleaf基础语法及使用

引入标签

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

引入URL

@{...}

例如:

<a th:href="@{http://www.baidu.com}">绝对路径</a> 是访问绝对路径下的URL, <a th:href="@{/}">相对路径</a> 是访问相对路径下的URL。 <a th:href="@{css/bootstrap.min.css}">是引入默认的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}" th:unless=${user.number != null}>Login</a>

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

<p 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> </p>

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    …

5.小结

本文讲述了如何在Spring Boot中引入模板引擎Thymeleaf以及Thymeleaf基础语法和实际使用

【相关推荐:Java视频教程】

以上就是SpringBoot引入Thymeleaf的方法介绍(代码)的详细内容,更多内容请关注技术你好其它相关文章!

来源链接:
免责声明:
1.资讯内容不构成投资建议,投资者应独立决策并自行承担风险
2.本文版权归属原作所有,仅代表作者本人观点,不代表本站的观点或立场
标签: thymeleaf
上一篇:php获取远程图片并下载保存到本地的方法分析 下一篇:Java8的CompletableFuture的用法介绍(附示例)

相关资讯