宁波网站建设 熊掌号,互联网门户网站,公司网站备案需要什么,昌邑网站建设公司一#xff0c;什么情况下会用到嵌套循环#xff1f;当我们展示多个分类时#xff0c;每个分类下又展示出推荐的前几个商品#xff0c;这时我们需要用到嵌套循环看一个例子#xff1a;说明#xff1a;作者:刘宏缔 邮箱: 371125307qq.com二#xff0c;演示项目的相关信息1…一什么情况下会用到嵌套循环当我们展示多个分类时每个分类下又展示出推荐的前几个商品这时我们需要用到嵌套循环看一个例子说明作者:刘宏缔 邮箱: 371125307qq.com二演示项目的相关信息1,项目地址:https://github.com/liuhongdi/templateloop2,项目功能说明演示了模板中常用的嵌套循环3,项目结构如图三配置文件说明1,pom.xmlorg.springframework.bootspring-boot-starter-thymeleaf2,application.properties#errorserver.error.include-stacktracealways#errorlogging.level.org.springframework.webtrace#thymeleafspring.thymeleaf.cachefalsespring.thymeleaf.encodingUTF-8spring.thymeleaf.modeHTMLspring.thymeleaf.prefixclasspath:/templates/spring.thymeleaf.suffix.html四java程序说明1,Category.java//分类模型public classCategory {//分类idLong categoryId;publicLong getCategoryId() {return this.categoryId;}public voidsetCategoryId(Long categoryId) {this.categoryId categoryId;}//分类名称privateString categoryName;publicString getCategoryName() {return this.categoryName;}public voidsetCategoryName(String categoryName) {this.categoryName categoryName;}//打印publicString toString(){return Category:categoryId categoryId categoryName categoryName;}}2,Goods.java//商品模型public classGoods {//商品idLong goodsId;publicLong getGoodsId() {return this.goodsId;}public voidsetGoodsId(Long goodsId) {this.goodsId goodsId;}//商品名称privateString goodsName;publicString getGoodsName() {return this.goodsName;}public voidsetGoodsName(String goodsName) {this.goodsName goodsName;}//商品标题privateString subject;publicString getSubject() {return this.subject;}public voidsetSubject(String subject) {this.subject subject;}//商品价格privateBigDecimal price;publicBigDecimal getPrice() {return this.price;}public voidsetPrice(BigDecimal price) {this.price price;}//库存intstock;public intgetStock() {return this.stock;}public void setStock(intstock) {this.stock stock;}//打印publicString toString(){return Goods:goodsId goodsId goodsName goodsName subject subject price price stock stock;}}3,HomeController.javaControllerRequestMapping(/home)public classHomeController {//返回分栏目的商品列表GetMapping(/category)publicString home(Model model) {//统一使用一个list返回ArrayList list new ArrayList();//每个分类及分类下的推荐商品用一个map来保存//第一个分类Map map1 new HashMap();Category category1 newCategory();category1.setCategoryId(1L);category1.setCategoryName(家居);map1.put(category,category1);//保存商品用的listArrayList listGoods1 new ArrayList();Goods goods1 newGoods();goods1.setGoodsId(1L);goods1.setGoodsName(无线智能感应灯);listGoods1.add(goods1);Goods goods2 newGoods();goods2.setGoodsId(2L);goods2.setGoodsName(朱之光落地灯);listGoods1.add(goods2);Goods goods3 newGoods();goods3.setGoodsId(3L);goods3.setGoodsName(儿童抗首菌枕头);listGoods1.add(goods3);Goods goods4 newGoods();goods4.setGoodsId(4L);goods4.setGoodsName(按摩床垫升级款);listGoods1.add(goods4);Goods goods5 newGoods();goods5.setGoodsId(5L);goods5.setGoodsName(北欧简约金属茶几);listGoods1.add(goods5);map1.put(goodslist,listGoods1);//把map1添加到listlist.add(map1);//第二个分类Map map2 new HashMap();Category category2 newCategory();category2.setCategoryId(2L);category2.setCategoryName(美护);map2.put(category,category2);ArrayList listGoods2 new ArrayList();Goods goods21 newGoods();goods21.setGoodsId(21L);goods21.setGoodsName(护手霜套装);listGoods2.add(goods21);Goods goods22 newGoods();goods22.setGoodsId(22L);goods22.setGoodsName(美白牙贴);listGoods2.add(goods22);Goods goods23 newGoods();goods23.setGoodsId(23L);goods23.setGoodsName(口腔护理泡沫);listGoods2.add(goods23);map2.put(goodslist,listGoods2);//把map2添加到listlist.add(map2);//第三个分类Map map3 new HashMap();Category category3 newCategory();category3.setCategoryId(3L);category3.setCategoryName(服装);map3.put(category,category3);ArrayList listGoods3 new ArrayList();Goods goods31 newGoods();goods31.setGoodsId(31L);goods31.setGoodsName(纯色真丝睡袍);listGoods3.add(goods31);Goods goods32 newGoods();goods32.setGoodsId(32L);goods32.setGoodsName(蚕丝条纹睡衣套装);listGoods3.add(goods32);Goods goods33 newGoods();goods33.setGoodsId(33L);goods33.setGoodsName(牛津长袖衬衫);listGoods3.add(goods33);map3.put(goodslist,listGoods3);把map3添加到listlist.add(map3);//把list传递给模板model.addAttribute(list,list);return home/category.html;}}说明我们没有使用数据库因为只是用于演示直接用代码写入了11条数据共三个分类每个分类的数据各放入一个map中最后用一个list传递给模板4category.html