Thymeleaf配合springboot实现遍历

导读:本篇文章讲解 Thymeleaf配合springboot实现遍历,希望对大家有帮助,欢迎收藏,转发!站点地址:www.bmabk.com

1:控制层代码,写一个数组集合

请添加图片描述

2:视图层:写th:each这里类似于vue语法

请添加图片描述

结果展示:

请添加图片描述

源码:控制层

package com.example.demo.controller;


import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;

import java.util.Arrays;


/**
 * @author ${范涛之}
 * @Description
 * @create 2021-09-11 20:31
 * 自动装配
 * 本身就是spring的组件
 */
@Controller
public class HelloController {
    @GetMapping("/test")
    public  String test(Model model){
        model.addAttribute("msg","<h1>helloftzdsj</h1>");


        model.addAttribute("users", Arrays.asList("fantaozhi","dengsijia"));

        return "index";
    }


}

源码:视图层

<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
<div th:text="${msg}"></div>
<div th:utext="${msg}"></div>   //转意

<hr>
<h3 th:each="user:${users}"  th:text="${user}"></h3>
</body>
</html>

同样可以在视图层这样写:
使用中括号(但是不建议)

<h3 th:each="user:${users}"> [[${user}]]</h3>

实现了同样的效果

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。

文章由半码博客整理,本文链接:https://www.bmabk.com/index.php/post/81100.html

(0)
小半的头像小半

相关推荐

半码博客——专业性很强的中文编程技术网站,欢迎收藏到浏览器,订阅我们!