【SpringBoot】SpringBoot整合thymeleaf

  • 官网:Thymeleaf is a modern server-side Java template engine for both web and standalone environments.
  • 翻译:Thymeleaf是适用于Web和独立环境的现代服务器端 Java 模板引擎

Thymeleaf可以轻松与SpringMvc、springBoot等web框架进行集成,适用于 Web 和独立环境开发。与jsp、Velocity、FreeMaker功能类似,thymeleaf可以通过thymeleaf标签渲染处理数据用以展示给用户。

目前企业项目基本都是前后端分离模式,但thymeleaf模板对于行业初学者学习或个人承接项目使用都还是个不错的选择。

SpringBoot整合thymeleaf

导入依赖

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

配置文件

server:
  port: 2023
spring:
  thymeleaf:
    # 视图解析前缀
    prefix: classpath:/templates/
    # 视图解析后缀
    suffix: .html
    # 模板编码
    encoding: UTF-8
    # 关闭缓存
    cache: false
    servlet:
      content-type: text/html

在templates文件夹下创建index.html

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>主页</title>
</head>
<body>
    <h4>SpringBoot整合thymeleaf主页面测试!</h4>
</body>
</html>

创建视图跳转控制器

注意:templates下的文件不能通过浏览器地址栏直接访问,需要通过控制器跳转

/**
 * @Project: spring-boot-thymeleaf-demo
 * @Author: cxs2014501@163.com
 * @Create: 2023/2/15 13:58
 * @Description:
 **/
@Controller
public class ViewController {

    @GetMapping("/index")
    public ModelAndView index(ModelAndView modelAndView){
        modelAndView.setViewName("index");
        return modelAndView;
    }
}

浏览器访问:127.0.0.1:2023/index

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-I86y2Xt4-1682596608742)(pictures/image-20230215140148511.png)]

thymeleaf语法介绍
表达式说明
表达式名字语法用途
变量取值${…}获取请求作用域、会话作用域、对象值
选择变量*{…}获取上下文对象值
消息#{…}获取国际化等值
链接@{…}生成链接
片段表达式~{…}引入公共页面片段
简单语法说明

设置单个值

<form action="subscribe.html" th:attr="action=@{/subscribe}">
  <fieldset>
    <input type="text" name="email" />
    <input type="submit" value="Subscribe!" th:attr="value=#{subscribe.submit}"/>
  </fieldset>
</form>

简化写法th:xxx

<input type="submit" value="Subscribe!" th:value="#{subscribe.submit}"/>
<form action="subscribe.html" th:action="@{/subscribe}">

迭代循环遍历

<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>
<tr th:each="prod,iterStat : ${prods}" th:class="${iterStat.odd}? 'odd'">
  <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>

条件判断

<a href="comments.html"
th:href="@{/product/comments(prodId=${prod.id})}"
th:if="${not #lists.isEmpty(prod.comments)}">view</a>

选择结构

<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>
简单案例
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>

依赖包会自动进行thymeleaf配置

1、所有thymeleaf的配置值都在 ThymeleafProperties

2、配置好了 SpringTemplateEngine

3、配好了 ThymeleafViewResolver

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-Az7lCYIP-1682596608743)(pictures/image-20230215141707839.png)]

如果配置文件没有进行相关配置,默认配置如下

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-ThJ2uBXH-1682596608743)(pictures/image-20230215141748373.png)]

在html页面加入命名空间

<html lang="en" xmlns:th="http://www.thymeleaf.org">

demo.html

<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>demo案例</title>
</head>
<body>
    <div>
        <h2 th:text="${msg}"></h2>
        <h4>
            <a href="www.taobao.com" th:href="${link}">去百度</a><br/>
        </h4>
    </div>
</body>
</html>

ViewController

@Controller
public class ViewController {

    @GetMapping("/demo")
    public ModelAndView demo(ModelAndView modelAndView){
        modelAndView.setViewName("demo");
        modelAndView.addObject("msg", "Hello, Thymeleaf!");
        modelAndView.addObject("link", "https://www.baidu.com");
        return modelAndView;
    }
}
结语

1、制作不易,一键三连再走吧,您的支持永远是我最大的动力!

2、Java全栈技术交流Q群:941095490,欢迎您的加入!

3、源码地址:https://gitee.com/whole-stack-of-white/spring-boot-thymeleaf-demo

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

全栈小白.

感谢老板,祝老板今年发大财!

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值