【问题标题】:Spring MVC JSP select tag - List objects GROUP BYSpring MVC JSP 选择标签 - 列出对象 GROUP BY
【发布时间】:2015-04-03 21:56:09
【问题描述】:

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

用户(id、名字、姓氏、年龄)。

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

Spring 控制器方法:

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

有人可以帮忙解决吗?谢谢。

【问题讨论】:

标签: java spring jsp


【解决方案1】:

假设 userService.groupByAge() 返回一个MAP&lt;Integer, User&gt; 那么你可以在jsp中列出如下

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

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-02-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-09-02
    • 2021-01-15
    • 2011-05-08
    • 1970-01-01
    相关资源
    最近更新 更多