SpringBoot整合Thymeleaf(十三)下

简介: SpringBoot整合Thymeleaf(十三)

三.四 each 循环 显示


三.四.一 后端响应


/**
     * for 循环展示
     * @param model
     * @return
     */
    @RequestMapping("/for")
    public String forModel(Model model){
        model.addAttribute("title","学习 Thymeleaf的If标签");
        //放置 java bean 对象
        User user=new User();
        user.setName("岳泽霖");
        user.setAge(26);
        user.setDescription("一个快乐的程序员");
        model.addAttribute("user",user);
        //放置if 语句
        //放置foreach 语句
        String[] hobbies=new String[]{"看书","编程","打游戏"};
        model.addAttribute("hobbies",hobbies);
        List<User> userList=getUserList();
        model.addAttribute("userList",userList);
        return "for";
    }


返回到首页


三.四.二 前端页面 for.html


<!doctype html>
<!--注意:引入thymeleaf的名称空间-->
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta http-equiv="Content-Type"content="text/html;charset=UTF-8">
    <title>Welcome <span th:text="${title}"/></title>
    <link rel="StyleSheet" th:href="@{webjars/bootstrap/3.4.1/css/bootstrap.css}" type="text/css">
</head>
<body class="container">
<h1>Java Bean 展示信息</h1>
我的名字是: <span th:text="${user.name}"/>
    年龄是: <span th:text="${user['age']}"/>
    描述是: <textarea th:block th:text="${user.getDescription()}"/>
<h1>循环 th:each 的处理</h1>
我的爱好:
<div th:each="hobby:${hobbies}">
    <span th:text="${hobby}"></span>
</div>
<table class="table table-hover">
    <th>
        <td>id编号</td>
        <td>名称</td>
        <td>年龄</td>
        <td>性别</td>
        <td>描述</td>
    </th>
    <th:block th:each="u:${userList}">
    <tr>
        <td></td>
        <td th:text="${u.id}"></td>
        <td th:text="${u.name}"></td>
        <td th:text="${u.age}"></td>
        <td th:text="${u.sex=='男'}?'男':'女'">
        </td>
        <td th:text="${u.description}"></td>
    </tr>
    </th:block>
</table>
<script type="text/javascript" th:src="@{webjars/jquery/3.5.1/jquery.js}"></script>
<script type="text/javascript" th:src="@{webjars/bootstrap/3.4.1/js/bootstrap.js}"></script>
</body>
</html>


引入 thymeleaf 的名称空间


<!--注意:引入thymeleaf的名称空间-->
<html lang="en" xmlns:th="http://www.thymeleaf.org">


th:each 表示循环处理


也可以


 <th:block th:each="u,stat:${userList}">
    <tr>
        <td></td>
        <td th:text="${u.id}"></td>
        <td th:text="${u.name}"></td>
        <td th:text="${u.age}"></td>
        <td th:text="${u.sex=='男'}?'男':'女'">
        </td>
        <td th:text="${u.description}"></td>
    </tr>
    </th:block>


stat对象包含以下属性:


  • index,从0开始的角标
  • count,元素的个数,从1开始
  • size,总元素个数
  • current,当前遍历到的元素
  • even/odd,返回是否为奇偶,boolean值
  • first/last,返回是否为第一或最后,boolean值


进行处理.


三.四.三 响应展示


通过控制器 进行响应,会将数据展示出来。


7a7b9f4204c3113fb45409b18a7d7709.png


三.五 js里面引入数据显示


三.五.一 后端响应


@RequestMapping("/myjs")
    public String myjs(Model model){
        model.addAttribute("title","学习 Thymeleaf的If标签");
        //放置 java bean 对象
        User user=new User();
        user.setName("岳泽霖");
        user.setAge(26);
        user.setDescription("一个快乐的程序员");
        model.addAttribute("user",user);
        return "myjs";
    }


返回到首页


三.五.二 前端页面 myjs.html


<!doctype html>
<!--注意:引入thymeleaf的名称空间-->
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta http-equiv="Content-Type"content="text/html;charset=UTF-8">
    <title>Welcome <span th:text="${title}"/></title>
    <link rel="StyleSheet" th:href="@{webjars/bootstrap/3.4.1/css/bootstrap.css}" type="text/css">
</head>
<body class="container">
<h1>Java Bean 展示信息</h1>
我的名字是: <span th:text="${user.name}"/>
    年龄是: <span th:text="${user['age']}"/>
    描述是: <textarea th:block th:text="${user.getDescription()}"/>
<script th:inline="javascript">
    const user = /*[[${user}]]*/ {};
    const age = /*[[${user.age}]]*/ 24;
    const name = /*[[${user.name}]]*/ "两个蝴蝶飞";
    console.log(user);
    console.log(age)
    console.log(name)
</script>
<script type="text/javascript" th:src="@{webjars/jquery/3.5.1/jquery.js}"></script>
<script type="text/javascript" th:src="@{webjars/bootstrap/3.4.1/js/bootstrap.js}"></script>
</body>
</html>


引入 thymeleaf 的名称空间


<!--注意:引入thymeleaf的名称空间-->
<html lang="en" xmlns:th="http://www.thymeleaf.org">


使用 th:inline , 通过 /*[[Thymeleaf表达式]]*/ 默认值 进行处理


三.五.三 响应展示


通过控制器 进行响应,会将数据展示出来。


3de1344440b517e39a53b29806b1869a.png


本章节的代码放置在 github 上:


https://github.com/yuejianli/springboot/tree/develop/SpringBoot_Thymeleaf


谢谢您的观看,如果喜欢,请关注我,再次感谢 !!!

相关文章
|
JSON JavaScript 数据可视化
SpringBoot+Thymeleaf+ECharts实现大数据可视化(基础篇)
SpringBoot+Thymeleaf+ECharts实现大数据可视化(基础篇)
833 0
SpringBoot+Thymeleaf+ECharts实现大数据可视化(基础篇)
|
缓存 JavaScript 前端开发
SpringBoot——Thymeleaf中的th:inline(内敛文本text、内敛脚本javascript)
SpringBoot——Thymeleaf中的th:inline(内敛文本text、内敛脚本javascript)
2201 0
SpringBoot——Thymeleaf中的th:inline(内敛文本text、内敛脚本javascript)
|
缓存 编解码 移动开发
SpringBoot 整合 Thymeleaf|学习笔记
快速学习 SpringBoot 整合 Thymeleaf
153 0
SpringBoot 整合 Thymeleaf|学习笔记
|
安全 前端开发 Java
狂神说SpringBoot:SpringSecurity笔记及thymeleaf静态资源(三)
狂神说SpringBoot:SpringSecurity笔记及thymeleaf静态资源(三)
142 0
狂神说SpringBoot:SpringSecurity笔记及thymeleaf静态资源(三)
|
移动开发 安全 前端开发
狂神说SpringBoot:SpringSecurity笔记及thymeleaf静态资源(二)
狂神说SpringBoot:SpringSecurity笔记及thymeleaf静态资源(二)
209 0
狂神说SpringBoot:SpringSecurity笔记及thymeleaf静态资源(二)
|
开发框架 安全 Java
狂神说SpringBoot:SpringSecurity笔记及thymeleaf静态资源(一)
狂神说SpringBoot:SpringSecurity笔记及thymeleaf静态资源(一)
441 0
|
XML 移动开发 前端开发
Springboot整合Thymeleaf
Springboot整合Thymeleaf
139 0
|
消息中间件 JSON 前端开发
SpringBoot2.x系列教程08--SpringBoot中整合Thymeleaf动态模板
前言 在前面的章节中,壹哥 带各位利用SpringBoot实现了SSM整合,发现现在SSM整合变得的非常简单,通过几个配置就能搭建出一个项目开发环境。但是在之前的案例中,我们并没有提供UI界面,那么在SpringBoot中如何整合UI界面呢?使用JSP展示页面,还是用HTML展示页面,或者还有其他方案? 在今天的内容里,壹哥 会带大家学习如何在SpringBoot中展示UI界面,这样大家以后就可以把数据信息在页面上渲染展示了。 一. Web开发方式简介 SpringBoot作为一个简化项目开发的利器,其实它为我们提供了一套完整的Web开发方案,从前端到后端,再到数据库、定时任务、消息队列等都
234 0
|
Dubbo Java 应用服务中间件
SpringBoot——借助Maven多模块管理实现集成SSM、Dubbo、Thymeleaf的汇总案例
SpringBoot——借助Maven多模块管理实现集成SSM、Dubbo、Thymeleaf的汇总案例
SpringBoot——借助Maven多模块管理实现集成SSM、Dubbo、Thymeleaf的汇总案例
SpringBoot——Thymeleaf中的表达式基本对象、表达式功能对象
SpringBoot——Thymeleaf中的表达式基本对象、表达式功能对象
SpringBoot——Thymeleaf中的表达式基本对象、表达式功能对象