SpringBoot自带模板引擎Thymeleaf使用详解②

这篇具有很好参考价值的文章主要介绍了SpringBoot自带模板引擎Thymeleaf使用详解②。希望对大家有所帮助。如果存在错误或未考虑完全的地方,请大家不吝赐教,您也可以点击"举报违法"按钮提交疑问。

目录

一、条件判断和迭代遍历

1.1 条件判断

2.2 迭代遍历

二、获取域中的数据和URL写法

2.1 获取域中的数据

2.2 URL写法

三、相关配置


一、条件判断和迭代遍历

1.1 条件判断

语法 作用
th:if 条件判断

准备数据

model.addAttribute("sex","男");

使用实例

<div>
    <span th:if="${sex}=='女'">这是女生</span>
    <span th:if="${sex}=='男'">这是男生</span>
</div>

运行结果: 

SpringBoot自带模板引擎Thymeleaf使用详解②,SpringBoot,spring boot,后端,java,thymeleaf,原力计划

当然还有th:case也是相当于Java中的switch

添加数据

model.addAttribute("id",2);

使用实例

<div th:switch="${id}">
    <span th:case="1">id为1</span>
    <span th:case="2">id为2</span>
    <span th:case="3">id为3</span>
    <span th:case="*">id为*</span>
</div>

运行结果

SpringBoot自带模板引擎Thymeleaf使用详解②,SpringBoot,spring boot,后端,java,thymeleaf,原力计划

2.2 迭代遍历

编写实体类

package com.example.springbootdemo2.pojo;

public class User {
    private int id;
    private String name;
    private int age;

    public User() {
    }

    public User(int id, String name, int age) {
        this.id = id;
        this.name = name;
        this.age = age;
    }

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }
}

准备数据

// 添加List列表集合
User user1 = new User(1,"张三",100);
User user2 = new User(2,"李四",90);
User user3 = new User(3,"王五",60);
User user4 = new User(4,"老六",29);
List<User> users = new ArrayList<>();
users.add(user1);
users.add(user2);
users.add(user3);
users.add(user4);
model.addAttribute("users",users); 

在页面中展示数据且配合状态变量

thymeleaf将遍历的状态变量封装到一个对象中,通过该对象的属性可以获取状态变量:

状态变量常用属性
状态变量 含义
index 当前迭代器的索引,从0开始
count 当前迭代对象的计数,从1开始
size 被迭代对象的长度
odd/even 布尔值,当前循环是否是偶数/奇数,从0开始
first 布尔值,当前循环的是否是第一条,如果是返回true,否则返回false
last 布尔值,当前循环的是否是最后一条,如果是则返回true,否则返回false

使用实例

<table border="1">
    <tr>
        <th>id</th>
        <th>姓名</th>
        <th>年龄</th>
        <th>当前迭代器的索引,从0开始</th>
        <th>当前迭代对象的计数,从1开始</th>
        <th>被迭代对象的长度</th>
        <th>布尔值,当前循环是否是偶数,从0开始</th>
        <th>布尔值,当前循环是否是奇数,从0开始</th>
        <th>布尔值,当前循环的是否是第一条,如果是返回true,否则返回false</th>
        <th>布尔值,当前循环的是否是最后一条,如果是则返回true,否则返回false</th>
    </tr>
    <tr th:each="user,status: ${users}">
        <td th:text="${user.getId()}"></td>
        <td th:text="${user.getName()}"></td>
        <td th:text="${user.getAge()}"></td>
        <td th:text="${status.index}"></td>
        <td th:text="${status.count}"></td>
        <td th:text="${status.size}"></td>
        <td th:text="${status.odd}"></td>
        <td th:text="${status.even}"></td>
        <td th:text="${status.first}"></td>
        <td th:text="${status.last}"></td>
    </tr>
</table>

运行结果: 

SpringBoot自带模板引擎Thymeleaf使用详解②,SpringBoot,spring boot,后端,java,thymeleaf,原力计划

遍历Map

准备数据

// 添加map集合数据
Map<String,User> userMap = new HashMap<>();
userMap.put("user1",user1);
userMap.put("user2",user2);
userMap.put("user3",user3);
userMap.put("user4",user4);
model.addAttribute("userMap",userMap);

使用实例 

<table>
    <tr>
        <th>ID</th>
        <th>Name</th>
        <th>Age</th>
        <th>Key</th>
    </tr>
    <tr th:each="m : ${userMap}">
        <td th:text="${m.value.getId()}"></td>
        <td th:text="${m.value.getName()}"></td>
        <td th:text="${m.value.getAge()}"></td>
        <td th:text="${m.key}"></td>
    </tr>
</table>

运行结果: 

SpringBoot自带模板引擎Thymeleaf使用详解②,SpringBoot,spring boot,后端,java,thymeleaf,原力计划

二、获取域中的数据和URL写法

2.1 获取域中的数据

thymeleaf也可以获取request,session,application域中的数据,方法如下:

准备数据

// 往request域设置数据
req.setAttribute("req","request");
// 往response域设置数据
session.setAttribute("session","session");
// 往application域设置数据
session.getServletContext().setAttribute("app","application");

使用实例

request域获取方式1: <span th:text="${#request.getAttribute('req')}"></span>
request域获取方式2: <span th:text="${#httpServletRequest.getAttribute('req')}"></span>
<hr>
session域获取方式1: <span th:text="${#session.getAttribute('session')}"></span>
session域获取方式2: <span th:text="${#httpSession.getAttribute('session')}"></span>
<hr>
application域获取方式1: <span th:text="${application.app}"></span>
application域获取方式2: <span th:text="${#servletContext.getAttribute('app')}"></span>
<hr>

运行结果:

SpringBoot自带模板引擎Thymeleaf使用详解②,SpringBoot,spring boot,后端,java,thymeleaf,原力计划

2.2 URL写法

在Thymeleaf中路径的写法为 @{路径},同样也可以在路径中添加参数,使用RestFul样式URL。

准备数据

model.addAttribute("id",100);
model.addAttribute("name","lyl");

添加跳转路径

@GetMapping("/show2")
@ResponseBody
public String showPage2(String id,String name){
    return id+":"+name;
}

// @RestFul风格传递参数
@GetMapping("/show3/{id}/{name}")
@ResponseBody
public String showPage3(@PathVariable String id,@PathVariable String name){
    return id + ":" + name;
}

使用实例

<a th:href="@{https://www.baidu.com}">百度</a>
<a th:href="@{show2?id=1&name='lyl'}">静态参数一</a>
<a th:href="@{show2(id=1,name='hqx')}">静态参数二</a>
<a th:href="@{'show2?id='+${id}+'&name='+${name}}">动态参数一</a>
<a th:href="@{show2(id=${id},name=${name})}">动态参数二</a>
<a th:href="@{show3/{id}/{name}(id=${id},name=${name})}">RestFul风格传递参数</a><hr>

运行结果

SpringBoot自带模板引擎Thymeleaf使用详解②,SpringBoot,spring boot,后端,java,thymeleaf,原力计划

三、相关配置

在Springboot配置文件中可以进行Thymeleaf相关配置

thymeleaf相关配置项
配置项 含义
spring.thymeleaf.prefix 视图前缀
spring.thymeleaf.suffix 视图后缀
spring.thymeleaf.encoding 编码格式
spring.thymeleaf.servlet.content-type 响应类型
spring.thymeleaf.cache=false 页面缓存,配置为false则不启用页面缓存,方便测试

SpringBoot自带模板引擎Thymeleaf使用详解②,SpringBoot,spring boot,后端,java,thymeleaf,原力计划文章来源地址https://www.toymoban.com/news/detail-715939.html

到了这里,关于SpringBoot自带模板引擎Thymeleaf使用详解②的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处: 如若内容造成侵权/违法违规/事实不符,请点击违法举报进行投诉反馈,一经查实,立即删除!

领支付宝红包 赞助服务器费用

相关文章

  • SpringBoot整合模板引擎Thymeleaf(4)

    本文原创作者:谷哥的小弟 作者博客地址:http://blog.csdn.net/lfdfhl 在之前的教程中,我们介绍了Thymeleaf的基础知识。在此,以案例形式详细介绍Thymeleaf的基本使用。 要点概述: 1、在static下创建css文件夹用于存放css文件 2、在static下创建img文件夹用于存放图片文件 请在pom.xml文

    2024年02月10日
    浏览(34)
  • 【SpringBoot学习笔记】04. Thymeleaf模板引擎

     所有的html元素都可以被thymeleaf替换接管  th:元素名 templates下的只能通过Controller来跳转,templates前后端分离,需要模板引擎thymeleaf支持    模板引擎的作用就是我们来写一个页面模板,比如有些值呢,是动态的,我们写一些表达式。而这些值,从哪来呢,就是我们在后台封

    2024年02月13日
    浏览(27)
  • 【Springboot】SpringBoot基础知识及整合Thymeleaf模板引擎

    🌕博客x主页:己不由心王道长🌕! 🌎文章说明:spring🌎 ✅系列专栏:spring 🌴本篇内容:对SpringBoot进行一个入门学习及对Thymeleaf模板引擎进行整合(对所需知识点进行选择阅读呀~)🌴 ☕️每日一语:在人生的道路上,即使一切都失去了,只要一息尚存,你就没有丝毫理

    2023年04月23日
    浏览(33)
  • 15 springboot项目——thymeleaf语法与关闭模板引擎

            在html文件中,有些是需要使用本地的css样式,使用thymeleaf语法加载:         首先对head标签上面的html标签进行更改:         其次,导入thymeleaf依赖:         接着,使用thymeleaf语法:         碰到href或者src后边与静态资源有关的的本地路径要进行修改,把要

    2024年02月14日
    浏览(30)
  • 【Spring Boot】Thymeleaf模板引擎 — 表达式的语法

    模板的主要作用是将后台返回的数据渲染到HTML中。那么Thymeleaf是如何解析后台数据的呢?接下来从变量、方法、条件判断、循环、运算(逻辑运算、布尔运算、比较运算、条件运算)方面学习Thymeleaf表达式支持的语法。 (1)文本赋值 赋值就是通过${}标签将后台返回的数据替

    2024年02月14日
    浏览(27)
  • 前端模板引擎Thymeleaf的整合和使用

    目录 一、添加依赖 1.1首先,在项目的构建文件中(比如 Maven 或 Gradle)添加 Thymeleaf 的依赖。例如,对于 Maven 项目,在 pom.xml 文件中添加以下依赖 1.2保存并更新项目依赖 二、配置Thymeleaf 2.1模板位置配置 2.2模板缓存配置 2.3自定义标签配置 三、创建模板文件 3.1创建一个HTML文

    2024年04月27日
    浏览(42)
  • thymeleaf模板引擎

    ThymeleafProperties 配置类 1.默认编码 2.前缀 3.后缀 相当于视图解析器    这是学SpringBoot的必经之路,非常重要!!!(除非你是学前端的)   只改了前端代码点一下这个就可以刷新    传值过来了  th:text=\\\"${msg}\\\"爆红,但是可以显示,File-Settings-Editor-Inspection  取消“Expression

    2024年02月14日
    浏览(30)
  • 使用 Velocity 模板引擎的 Spring Boot 应用

    模板引擎是构建动态内容的重要工具,特别适用于生成HTML、邮件内容、报告和其他文本文档。Velocity是一个强大的模板引擎,它具有简单易用的语法和灵活性。本文将介绍如何在Spring Boot应用中使用Velocity模板引擎,并提供示例代码。 Velocity是一个用于生成文本输出的模板引擎

    2024年02月07日
    浏览(27)
  • 基于Springboot+thymeleaf旅游景区管理系统——LW模板

    基于java的旅游管理系统 随着我国经济的快速发展以及改革开放政策的不断完善,旅游已经成为了人们假期放松旅游的主要方式之一。我国也越来越重视旅游业的发展,出台了《关于促进全域旅游发展的指导意见》、《“十四五”文化和旅游发展规划》等政策予以扶持。旅游

    2024年02月11日
    浏览(36)
  • freemarker模板引擎详解以及使用方法

    哈喽!大家好,我是旷世奇才李先生 文章持续更新,可以微信搜索【小奇JAVA面试】第一时间阅读,回复【资料】更有我为大家准备的福利哟,回复【项目】获取我为大家准备的项目 相关阅读 面试官:Zookeeper是什么,它有什么特性与使用场景? 面试官:Redis如何实现持久化的

    2024年02月09日
    浏览(32)

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

博客赞助

微信扫一扫打赏

请作者喝杯咖啡吧~博客赞助

支付宝扫一扫领取红包,优惠每天领

二维码1

领取红包

二维码2

领红包