2015-04-03 19 views
1

我基於以下屬性的用戶表:Spring MVC的JSP select標籤 - 對象列表GROUP BY

用戶(ID,名字,姓氏,年齡)。

所有用戶都按年齡分組,每個年齡段必須在選擇標籤中列出。

春控制器的方法:

@RequestMapping(value = "/listByAge") 
public String listByAge(@ModelAttribute("user") User user, Model model){ 
    model.addAttribute("ages", userService.groupByAge()); 
    return "/listByAge"; 
} 

JSP表單,選擇標籤:

<form:form action="/usersByAge/${age}" method="POST"> 
     // The problem to list is here... 
     // I need to create a select tag 
     <button type="submit">List</button> 
</form:form> 

有人可以幫助解決這個問題?謝謝。

+0

看看這個[回覆](http://stackoverflow.com/a/24390678/1066779) – Rembo 2015-04-04 03:45:43

回答

0

假設userService.groupByAge()返回一個MAP<Integer, User>然後你就可以在jsp中列出它如下

<c:forEach var="age" items="${ages}"> 
    <option value="${age.key}">${age.value}</option> 
</c:forEach> 

通過這種方式,你可以從地圖迭代的年齡。