欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

freemarker循环获取list中map的值

程序员文章站 2022-07-14 09:52:00
...

1.数据结构

List<Map<String,Object>> mapList = Lists.newArrayList();
Long originalOrderId = returnObj.getOriginalOrderId();
List<Long> orderIdList = Lists.newArrayList();
orderIdList.add(originalOrderId);
List<Item> itemList = itemService.listByOrderIdList(orderIdList);
for (Item item : itemList) {
	
	Map<String,Object> map = Maps.newLinkedHashMap();
	if(item != null){
		Product product = item.getProduct();
		if(product == null){
			product = productService.get(item.getProductId());
		}
		if(product != null){
			map.put("productName", product.getName());
		}
		List<SpecJsonItem> specList = item.getSpecItemList();
		if(CollectionUtils.isEmpty(specList)){
			specList = JSON.parseArray(item.getSpecJson(), SpecJsonItem.class);
		}
		if(CollectionUtils.isNotEmpty(specList)){
			StringBuffer spec = new StringBuffer();
			for (SpecJsonItem specJsonItem : specList) {
				spec.append(specJsonItem.getName()).append(":").append(specJsonItem.getValue()).append(" ");
			}
			map.put("spec", spec);
		}
	}else{
		item = new Item();
	}
	map.put("item", item);
	mapList.add(map);
}

model.addAttribute("mapList", mapList);
2.前端页面循环

<#list mapList as map>
	<tr class="product_${map_index}">
		<td>商品名称:</td>
		<td>${map['productName']}</td>
		<td rowspan="2">商品图片:</td>
		<td rowspan="2"><img src="${imageDomain}/${map['item'].pic}@200w"></td>
	</tr>
	
	<tr class="product_${map_index}">
		<td>购买规格:</td>
		<td>${map['spec']}</td>
	</tr>
	
	<tr class="product_${map_index}">
		<td>购买价格:</td>
		<td>${map['item'].price}</td>
		<td>购买数量:</td>
		<td>${map['item'].quantity}</td>
	</tr>
</#list>



相关标签: freemarker