Thymeleaf入门介绍

Thymeleaf,众多模板中的一员。因为项目使用的Spring Boot,而thymeleaf是它的内置集成的模板引擎,使用了一下。简单易学,上手快。文档例子都很易读,够用了!

安装

在Spring Boot项目中加入依赖,编译。配置文件如下

Maven

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

Gradle

dependencies {     compile("org.springframework.boot:spring-boot-starter-thymeleaf") }

模板文件位置

默认情况下我们需要把编写的模板文件放在src/main/resources/templates目录下,如图

templates下面可以按工程需要建立子目录,例如图中的components子目录。

如果想要更换templates目录可以修改spring.thymeleaf.prefix配置项.参见后面的配置项。

还想了解更多引擎配置的知识,请参见

Themeleaf配置项

在application.properties里可以对Thymeleaf进行相关配置

# THYMELEAF (ThymeleafAutoConfiguration) spring.thymeleaf.check-template-location=true spring.thymeleaf.prefix=classpath:/templates/ spring.thymeleaf.excluded-view-names= # comma-separated list of view names that should be excluded from resolution spring.thymeleaf.view-names= # comma-separated list of view names that can be resolved spring.thymeleaf.suffix=.htmlspring.thymeleaf.mode=HTML5 spring.thymeleaf.encoding=UTF-8 spring.thymeleaf.content-type=text/html # ;charset=<encoding> is added spring.thymeleaf.cache=true # set to false for hot refresh

标准表达式语法

模板技术主要功能就是根据逻辑将后台模型中的对象与HTML结合,生成最终页面。标准表达式语法处理java对象在HTML中的结合问题。

文字

<p>   Now you are looking at a <span th:text="'working web application'">template file</span>. </p>

URL

使用绝对路径,,

<ol>   <li><a href="product/list.html" th:href="@{/product/list}">Product List</a></li>   <li><a href="order/list.html" th:href="@{/order/list}">Order List</a></li> </ol>

条件表达式

<tr th:class="${row.even}? 'even' : 'odd'">   ... </tr>

遍历

遍历prods集合,打印每个prod对象的属性

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

th:XX语法的支持请参考官方文档5.2小节的表,

更多的标记语法可以查看官方文档,例子说明都很详实。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值