Spring MVC和Checkboxes [英] Spring MVC and Checkboxes

查看:97
本文介绍了Spring MVC和Checkboxes的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Spring MVC 3.0并且无法完全看到此问题的所有部分:我的控制器将生成一个域对象列表。假设一个简单的User对象具有firstName,lastName,age和role属性。我想在表中输出该用户列表(每个属性一列),每行还有一个默认选中的复选框。然后,使用该页面的人可能会取消选择其中一些页面。当他们点击提交按钮时,我希望能够获取所选用户的列表并对其进行操作。

I'm using Spring MVC 3.0 and can't quite see all the parts to this problem: my controller will produce a list of domain objects. Let's say a simple User object with firstName, lastName, age, and role properties. I want to output that list of users in a table (one column per property), each row also having a checkbox which are all selected by default. The person using the page can then potentially deselect some of them. When they hit the submit button, I'd like to be able to take the list of selected users and do something with them.

我知道有一个表单:复选框在Spring中标记,但我不太清楚如何使用它以及如何在控制器中获得结果。

I know there is a form:checkboxes tag in Spring, but I can't quite see how to use it and how to get the results in the controller.

任何帮助或建议?

推荐答案

如果你用户对象有一个 id 字段,您可以像这样提交所选用户的ID(在这个简单的场景中,您甚至不需要Spring的表单标记):

If you User object has an id field, you can submit ids of selected users like this (you don't even need Spring's form tag for this simple scenario):

<form ...>
    <c:foreach var = "user" items = "${users}">
        <input type = "checkbox" name = "userIds" value = "${user.id}" checked = "checked" /> <c:out value = "${user.firstName}" /> ...
    </c:foreach>
    ...
</form>

-

@RequestMapping (...)
public void submitUsers(@RequestParam(value = "userIds", required = false) long[] userIds)
{
    ...
}

这篇关于Spring MVC和Checkboxes的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆