今天遇到一個問題,就是通過後臺傳過來一個List參數 ,list的類型爲List<Map<String,String>> ,我需要將這個list 中的map遍歷並顯示到HTML頁面上(包括map 的key 和value)。解決辦法如下
<table>
<#if list??>//判斷傳過來的List是否爲空
<#list list as map>
<#list map?keys as k>
<tr>
<td>${k}</td>
<#if map[k]="異常停止">
<td class="color_red">${map[k]}</td>
<#else>
<td class="color_green">${map[k]}</td>
</#if>
</tr>
</#list>
</#list>
</#if>
</table>
後臺的代碼如下:
@RequestMapping(value="/monitor", method = { RequestMethod.GET, RequestMethod.POST })
public String monitor(HttpServletRequest req,Model model)
throws ServletException, IOException {
System.out.println("this is monitor html");
List<Map<String,String>> list= new ArrayList<Map<String,String>>();
Map map=new HashMap();
Map map2=new HashMap();
map.put("own服務器", "運行正常");
map2.put("RSA服務器", "異常停止");
list.add(map);
list.add(map2);
model.addAttribute("list", list);
return "jiankong";
}
頁面顯示的結果如下,(其中前兩行的內容是自己寫死的兩條記錄,可以忽略)
就這樣了。有錯誤還望指教~~