map 重复添加会被覆盖

首先最开始我的代码 :

@RequestMapping("/index")
	@ResponseBody
	public Object index() throws Exception{
	    PageData pd=this.getPageData();	
	    Map<String,Object> map=new HashMap<String,Object>();
	    Map<String,Object> data=new HashMap<String,Object>();
	    //List<PageData> goodsList=new ArrayList<PageData>();
	    List<PageData> banner=indexService.findBanner(pd);
	    List<PageData> channel= indexService.findchannel(pd);
	    List<PageData> newGoodsList= goodsService.findnewgoods(pd);
	    List<PageData> hotGoodsList= goodsService.findhotgoods(pd);
	    List<PageData> brandList= goodsService.findbrand(pd);
	    List<PageData> topicList= topicService.findtopic(pd);
	    List<PageData> categoryList=  categoryService.findCategory(pd);
	    for(int i=0;i<categoryList.size();i++){
	       long id= (Long) categoryList.get(i).get("id");
	       pd.put("id", id); 
	       List<PageData> goodsList=  goodsService.findgoodsbyCategory(pd); 
	       //goodsList.addAll(goods);
	       data.put("goodsList",goodsList);
	    }
	    //data.put("goodsList",goodsList);
	    data.put("categoryList",categoryList);
	    data.put("topicList",topicList);
	    data.put("brandList",brandList);
	    data.put("banner",banner);
	    data.put("channel", channel);
	    data.put("newGoodsList",newGoodsList);
	    data.put("hotGoodsList",hotGoodsList);
		map.put("data", data);
		map.put("errno", 0);
		return map;
	}

 

 

修改之后:

@RequestMapping("/index")
	@ResponseBody
	public Object index() throws Exception{
	    PageData pd=this.getPageData();	
	    Map<String,Object> map=new HashMap<String,Object>();
	    Map<String,Object> data=new HashMap<String,Object>();
	    List<PageData> goodsList=new ArrayList<PageData>();
	    List<PageData> banner=indexService.findBanner(pd);
	    List<PageData> channel= indexService.findchannel(pd);
	    List<PageData> newGoodsList= goodsService.findnewgoods(pd);
	    List<PageData> hotGoodsList= goodsService.findhotgoods(pd);
	    List<PageData> brandList= goodsService.findbrand(pd);
	    List<PageData> topicList= topicService.findtopic(pd);
	    List<PageData> categoryList=  categoryService.findCategory(pd);
	    for(int i=0;i<categoryList.size();i++){
	       long id= (Long) categoryList.get(i).get("id");
	       pd.put("id", id); 
	       List<PageData> goods=  goodsService.findgoodsbyCategory(pd); 
	      goodsList.addAll(goods);
	    }
	    data.put("goodsList",goodsList);
	    data.put("categoryList",categoryList);
	    data.put("topicList",topicList);
	    data.put("brandList",brandList);
	    data.put("banner",banner);
	    data.put("channel", channel);
	    data.put("newGoodsList",newGoodsList);
	    data.put("hotGoodsList",hotGoodsList);
		map.put("data", data);
		map.put("errno", 0);
		return map;
	}
List<PageData> goodsList=new ArrayList<PageData>();
	    List<PageData> banner=indexService.findBanner(pd);
	    List<PageData> channel= indexService.findchannel(pd);
	    List<PageData> newGoodsList= goodsService.findnewgoods(pd);
	    List<PageData> hotGoodsList= goodsService.findhotgoods(pd);
	    List<PageData> brandList= goodsService.findbrand(pd);
	    List<PageData> topicList= topicService.findtopic(pd);
	    List<PageData> categoryList=  categoryService.findCategory(pd);
	    for(int i=0;i<categoryList.size();i++){
	       long id= (Long) categoryList.get(i).get("id");
	       pd.put("id", id); 
	       List<PageData> goods=  goodsService.findgoodsbyCategory(pd); 
	      goodsList.addAll(goods);
	    }
	    data.put("goodsList",goodsList);
	    data.put("categoryList",categoryList);
	    data.put("topicList",topicList);
	    data.put("brandList",brandList);
	    data.put("banner",banner);
	    data.put("channel", channel);
	    data.put("newGoodsList",newGoodsList);
	    data.put("hotGoodsList",hotGoodsList);
		map.put("data", data);
		map.put("errno", 0);
		return map;
	}

欢迎访问我的网站

 

 


这样添加的数据就不会被覆盖了

发布了233 篇原创文章 · 获赞 26 · 访问量 13万+
展开阅读全文

没有更多推荐了,返回首页

©️2019 CSDN 皮肤主题: 大白 设计师: CSDN官方博客

分享到微信朋友圈

×

扫一扫,手机浏览