【spring boot】spring整合thymeleaf

本文参考:https://developer.aliyun.com/article/769977

  1. thymeleaf的优点:实现动静分离,常规的编程使用jsp,jsp页面你要想看到页面效果,布局样式有没有问题就必须运行程序去访问url才能看到页面,而引入thymeleaf之后可以实现html文件直接查看布局,样式有没有问题。
  2. thymeleaf实现:在html页面中加入<html xmlns:th="http://www.thymeleaf.org">声明,并且配置pom.xml文件配置依赖
  3. springboot默认thymeleaf的html文件存放在templates(模板)下
  4. 了解一些thymeleaf的语法:${}model取值
  5. 可通过配置项配置thymeleaf,开发注意事项:要禁用thymeleaf的页面缓存,不然可能浏览器html不是新的

依赖添加

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

基本语法

在这里插入图片描述

<div th:th="${name}"></div>

从model当中取名为name的值

<h3 th:text="'PID:202091100'+${photo.id}"></h3>

灵活使用

<a th:href="@{/lookPhoto/}+${photo.id}">
   <img th:src="@{${photo.address}}" alt="img">
                                            </a>

链接跳转
引入css

 <link rel="stylesheet" th:href="@{index.css}">

引入JavaScript:

 <script type="text/javascript" th:src="@{index.js}"></script>

model存入map

<h2>Map取值</h2>
<table bgcolor="#8fbc8f" border="1">
    <tr>
        <td>place:</td>
        <td th:text="${map.get('place')}"></td>
    </tr>
    <tr>
        <td>feeling:</td>
        <td th:text="${map['feeling']}"></td>
    </tr>
</table>

消息表达式:#{…}
在这里插入图片描述
if判断:参考https://blog.csdn.net/weixin_38192427/article/details/108061869
控制是否显示

th:if="true"
th:if="${value}"

基本配置

禁用缓存这对开发来说很重要,配置编写在application.yaml文件当中
在这里插入图片描述

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值