Spring MVC JSP select标签 - 列出对象GROUP BY

时间:2015-04-03 21:56:09

标签: java spring jsp

我有一个基于以下属性的User表:

用户(ID,名字,姓氏,年龄)。

所有用户都按年龄分组,每个年龄段必须列在选择标记中。

Spring Controller方法:

@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>

有人可以帮忙解决这个问题吗?感谢。

1 个答案:

答案 0 :(得分:0)

假设userService.groupByAge()返回MAP<Integer, User>然后你可以在jsp中列出如下

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

通过这种方式,您可以从地图中迭代年龄。