spring boot: 用thymeleaf嵌套循环展示多层数据(spring boot 2.3.2)
一,什么情况下会用到嵌套循环?
当我们展示多个分类时,每个分类下又展示出推荐的前几个商品,
这时我们需要用到嵌套循环
看一个例子:

说明:刘宏缔的架构森林是一个专注架构的博客,地址:https://www.cnblogs.com/architectforest
对应的源码可以访问这里获取: https://github.com/liuhongdi/
说明:作者:刘宏缔 邮箱: 371125307@qq.com
二,演示项目的相关信息
1,项目地址:
https://github.com/liuhongdi/templateloop
2,项目功能说明:
演示了模板中常用的嵌套循环
3,项目结构;如图:

三,配置文件说明
1,pom.xml
<!--thymeleaf begin-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
<!--thymeleaf end-->
2,application.properties
#error
server.error.include-stacktrace=always
#error
logging.level.org.springframework.web=trace #thymeleaf
spring.thymeleaf.cache=false
spring.thymeleaf.encoding=UTF-8
spring.thymeleaf.mode=HTML
spring.thymeleaf.prefix=classpath:/templates/
spring.thymeleaf.suffix=.html
四,java程序说明
1,Category.java
//分类模型
public class Category { //分类id
Long categoryId;
public Long getCategoryId() {
return this.categoryId;
}
public void setCategoryId(Long categoryId) {
this.categoryId = categoryId;
} //分类名称
private String categoryName;
public String getCategoryName() {
return this.categoryName;
}
public void setCategoryName(String categoryName) {
this.categoryName = categoryName;
} //打印
public String toString(){
return " Category:categoryId=" + categoryId +" categoryName=" + categoryName;
}
}
2,Goods.java
//商品模型
public class Goods {
//商品id
Long goodsId;
public Long getGoodsId() {
return this.goodsId;
}
public void setGoodsId(Long goodsId) {
this.goodsId = goodsId;
} //商品名称
private String goodsName;
public String getGoodsName() {
return this.goodsName;
}
public void setGoodsName(String goodsName) {
this.goodsName = goodsName;
} //商品标题
private String subject;
public String getSubject() {
return this.subject;
}
public void setSubject(String subject) {
this.subject = subject;
} //商品价格
private BigDecimal price;
public BigDecimal getPrice() {
return this.price;
}
public void setPrice(BigDecimal price) {
this.price = price;
} //库存
int stock;
public int getStock() {
return this.stock;
}
public void setStock(int stock) {
this.stock = stock;
} //打印
public String toString(){
return " Goods:goodsId=" + goodsId +" goodsName=" + goodsName+" subject=" + subject+" price=" + price+" stock=" + stock;
}
}
3,HomeController.java
@Controller
@RequestMapping("/home")
public class HomeController { //返回分栏目的商品列表
@GetMapping("/category")
public String home(Model model) {
//统一使用一个list返回
ArrayList<Map<String,Object>> list = new ArrayList<>(); //每个分类及分类下的推荐商品,用一个map来保存
//第一个分类
Map<String,Object> map1 = new HashMap<String,Object>();
Category category1 = new Category(); category1.setCategoryId(1L);
category1.setCategoryName("家居");
map1.put("category",category1); //保存商品用的list
ArrayList<Goods> listGoods1 = new ArrayList<Goods>(); Goods goods1 = new Goods();
goods1.setGoodsId(1L);
goods1.setGoodsName("无线智能感应灯");
listGoods1.add(goods1);
Goods goods2 = new Goods();
goods2.setGoodsId(2L);
goods2.setGoodsName("朱之光落地灯");
listGoods1.add(goods2);
Goods goods3 = new Goods();
goods3.setGoodsId(3L);
goods3.setGoodsName("儿童抗首菌枕头");
listGoods1.add(goods3);
Goods goods4 = new Goods();
goods4.setGoodsId(4L);
goods4.setGoodsName("按摩床垫升级款");
listGoods1.add(goods4);
Goods goods5 = new Goods();
goods5.setGoodsId(5L);
goods5.setGoodsName("北欧简约金属茶几");
listGoods1.add(goods5);
map1.put("goodslist",listGoods1); //把map1添加到list
list.add(map1); //第二个分类
Map<String,Object> map2 = new HashMap<String,Object>();
Category category2 = new Category();
category2.setCategoryId(2L);
category2.setCategoryName("美护");
map2.put("category",category2);
ArrayList<Goods> listGoods2 = new ArrayList<Goods>(); Goods goods21 = new Goods();
goods21.setGoodsId(21L);
goods21.setGoodsName("护手霜套装");
listGoods2.add(goods21);
Goods goods22 = new Goods();
goods22.setGoodsId(22L);
goods22.setGoodsName("美白牙贴");
listGoods2.add(goods22);
Goods goods23 = new Goods();
goods23.setGoodsId(23L);
goods23.setGoodsName("口腔护理泡沫");
listGoods2.add(goods23);
map2.put("goodslist",listGoods2);
//把map2添加到list
list.add(map2); //第三个分类
Map<String,Object> map3 = new HashMap<String,Object>();
Category category3 = new Category();
category3.setCategoryId(3L);
category3.setCategoryName("服装");
map3.put("category",category3);
ArrayList<Goods> listGoods3 = new ArrayList<Goods>(); Goods goods31 = new Goods();
goods31.setGoodsId(31L);
goods31.setGoodsName("纯色真丝睡袍");
listGoods3.add(goods31);
Goods goods32 = new Goods();
goods32.setGoodsId(32L);
goods32.setGoodsName("蚕丝条纹睡衣套装");
listGoods3.add(goods32);
Goods goods33 = new Goods();
goods33.setGoodsId(33L);
goods33.setGoodsName("牛津长袖衬衫");
listGoods3.add(goods33);
map3.put("goodslist",listGoods3);
////把map3添加到list
list.add(map3); //把list传递给模板
model.addAttribute("list",list);
return "home/category.html";
}
}
说明:我们没有使用数据库,因为只是用于演示,
直接用代码写入了11条数据,共三个分类
每个分类的数据各放入一个map中,最后用一个list传递给模板
4,category.html
<!DOCTYPE html>
<html lang="en">
<head>
</head>
<body>
<div style="width:100%;height:30px;background:#ffffff;font-size: 16px;" ></div>
<div id="content" style="width:1040px;">
<div style="width:790px;float:left;margin-left:30px;">
<!--main begin-->
<div th:each="categoryOne:${list}" style="width:790px;font-size:16px;overflow: hidden;">
<!--显示分类信息-->
<div style="background:#eeeeee;width:790px;height:20px;line-height:20px;overflow: hidden;margin-top: 10px;">
<div style="width:200px;float:left;">
分类ID:<span th:text="${categoryOne.category.categoryId}"></span> 分类名称:<span th:text="${categoryOne.category.categoryName}"></span>
</div> <div style="width:200px;float:right;text-align: right;">
<a th:href="@{/goods/goodslist/(categoryid=${categoryOne.category.categoryId})}">更多...</a>
</div>
</div>
<!--显示分类下的商品列表信息-->
<div style="width:790px;height:100px;">
<div th:each="goodsOne:${categoryOne.goodslist}" style="width:250px;height:100px;float: left;background:#eeeeee;margin-left:10px;margin-top:10px;">
<div>商品ID:[[${goodsOne.goodsId}]]</div>
<div>商品名称:[[${goodsOne.goodsName}]]</div>
</div>
</div>
</div>
<!--main end-->
</div>
</div>
</body>
</html>
注意代码中用了th:each嵌套循环了两层,变量名分别用了categoryOne和goodsOne
五,测试效果
访问:
http://127.0.0.1:8080/home/category
返回:

循环展示了多个分类下的多个商品
六,查看spring boot版本
. ____ _ __ _ _
/\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
\\/ ___)| |_)| | | | | || (_| | ) ) ) )
' |____| .__|_| |_|_| |_\__, | / / / /
=========|_|==============|___/=/_/_/_/
:: Spring Boot :: (v2.3.2.RELEASE)
spring boot: 用thymeleaf嵌套循环展示多层数据(spring boot 2.3.2)的更多相关文章
- 77. Spring Boot Use Thymeleaf 3【从零开始学Spring Boot】
[原创文章,转载请注明出处] Spring Boot默认选择的Thymeleaf是2.0版本的,那么如果我们就想要使用3.0版本或者说指定版本呢,那么怎么操作呢?在这里要说明下 3.0的配置在spri ...
- spring boot整合Thymeleaf的那些坑(spring boot 学习笔记之四)
这里简单记录一下Thymeleaf配置和使用的步骤 1.修改pom文件,添加依赖 <dependency> <groupId>org.springframework.boot& ...
- Spring Boot☞ 使用Thymeleaf模板引擎渲染web视图
静态资源访问 在我们开发Web应用的时候,需要引用大量的js.css.图片等静态资源. 默认配置 Spring Boot默认提供静态资源目录位置需置于classpath下,目录名需符合如下规则: /s ...
- Spring Boot2(五):使用Spring Boot结合Thymeleaf模板引擎使用总结
一.Thymeleaf概述 一般来说,常用的模板引擎有JSP.Velocity.Freemarker.Thymeleaf . SpringBoot推荐的 Thymeleaf – 语法更简单,功能更强大 ...
- Spring Boot2 系列教程(九)Spring Boot 整合 Thymeleaf
虽然现在慢慢在流行前后端分离开发,但是据松哥所了解到的,还是有一些公司在做前后端不分的开发,而在前后端不分的开发中,我们就会需要后端页面模板(实际上,即使前后端分离,也会在一些场景下需要使用页面模板, ...
- Spring Boot 2.X(三):使用 Spring MVC + MyBatis + Thymeleaf 开发 web 应用
前言 Spring MVC 是构建在 Servlet API 上的原生框架,并从一开始就包含在 Spring 框架中.本文主要通过简述 Spring MVC 的架构及分析,并用 Spring Boot ...
- 极简 Spring Boot 整合 Thymeleaf 页面模板
虽然现在慢慢在流行前后端分离开发,但是据松哥所了解到的,还是有一些公司在做前后端不分的开发,而在前后端不分的开发中,我们就会需要后端页面模板(实际上,即使前后端分离,也会在一些场景下需要使用页面模板, ...
- Spring Boot整合Thymeleaf视图层
目录 Spring Boot整合Thymeleaf Spring Boot整合Thymeleaf 的项目步骤 Thymeleaf 语法详解 Spring Boot整合Thymeleaf Spring ...
- Spring Boot和Thymeleaf整合,结合JPA实现分页效果
在项目里,我需要做一个Spring Boot结合Thymeleaf前端模版,结合JPA实现分页的演示效果.做的时候发现有些问题,也查了现有网上的不少文档,发现能全栈实现的不多,所以这里我就把我的做法, ...
随机推荐
- 关于Nginx mmap(MAP_ANON|MAP_SHARED, 314572800)报错
mmap 报错解决 今天修改了一下测试环境的Nginx的nginx.conf,然后做检测的时候报了一个错误 /usr/local/bin/nginx -c /usr/local/etc/openres ...
- springboot2.x基础教程:动手制作一个starter包
上一篇博客介绍了springboot自动装配的原理.springboot本身有丰富的spring-boot-starter-xx集成组件,这一篇趁热打铁加深理解,我们利用springboot自动装配的 ...
- 《Linux 操作系统》Linux的常用命令操作大全
前言 在学习命令之前先学习我们该如何去学习linux 命令. 几乎每一个命令都有参数,每个参数的含义是什么,我们一般也不是全部都能记住,所以我们必须有一个可以知道每一个命令下各个参数的含义的方法. 命 ...
- 使用wireshark分析MQTT协议
网络上搜索到两种用wireshark工具分析MQTT协议的方法,都是使用wireshark插件,一种是Wireshark Generic Dissector:另一种是使用lua脚本插件(推荐使用这种方 ...
- tomcat在linux服务器启动时报错Java HotSpot(TM) 64-Bit Server VM warning: INFO: os::commit_memory(0x0000000794500000, 576716800, 0) failed; error='Cannot allocate memory' (errno=12)
原因是内存不足了 解决办法:1.top命令查看后台进程,看哪些可以关闭 2.ps -ef |grep tomcat看哪些不用的tomcat起着,也可以从这里关 3.加大这个tomcat的内存, 在ca ...
- Node.js 从零开发 web server博客项目[koa2重构博客项目]
web server博客项目 Node.js 从零开发 web server博客项目[项目介绍] Node.js 从零开发 web server博客项目[接口] Node.js 从零开发 web se ...
- Redis5设计与源码分析读后感(二)简单动态字符串SDS
一.引言 学习之前先了解几个概念: SDS定义:简单动态字符串,Redis的基本数据结构之一,用于储存字符串和整型数据. 二进制安全:C语言中用"\0"表示字符串结束,如果字符串本 ...
- 【Django】将多个querysets拼接,使用djangorestframework序列化
concern_set = models.Concern.objects.filter(user_id=1).values("concern_id") querysets = mo ...
- @Autowired,@Resource,@Qualifier,@Primary,@Inject的作用和区别
@Autowired注解的用法:可以用于构造器,方法,参数,字段进行属性注入,有一个required属性,默认是true,当改成false时,如果注入的属性在容器中不存在也不会报错@Resource该 ...
- IDEA 使用的配置
IDEA 使用 工欲善其事必先利其器,选择适合自己的 IDE,会让自己事倍功半.作为 Java 开发环境,有人喜欢 Eclipse,有人喜欢 idea,这其中的差别对比这里不做对比,需要了解的朋友可以 ...