1. 程式人生 > >freemarker迴圈獲取list中map的值

freemarker迴圈獲取list中map的值

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>