1

我有这个页面:

    <form method="post" action="/monitor/admin/transferDevice/${device.deviceId}" >
        <input type="submit" value="Transfer Device">
    </form>

<form:select path="users">
    <option value="">Select</option>
    <c:forEach var="theUser" items="${users}">
        <form:option value="${theUser.userId}"><c:out value="${theUser.name} ${theUser.surname}"/></form:option>
    </c:forEach>
</form:select>

users用户对象列表在哪里。

我的控制器:

@RequestMapping(value = "/admin/transferDevice/{deviceId}", method = RequestMethod.POST)
public String transferForDevice(@PathVariable("deviceId") int deviceId) throws Exception {

    //some logic here

    return "redirect:/admin";
}

所以这个问题看起来很简单,但对我来说不是。当我按下按钮时,如何将选定的用户传递给我的控制器方法?

4

2 回答 2

3

我找到了我的问题的解决方案。所以这里是我更新的页面:

<form:form modelAttribute="selectedUser" method="POST" action="/monitor/admin/transferDevice/${device.deviceId}" style="width: 310px;">
   <input type="submit" value="Transfer Device" style="height: 68px; width: 197px; ">
        <tr>
            <td>User:</td>
            <td><form:select path="userId">
                <form:option value="0" label="--- Select ---" />
                <c:forEach var="theUser" items="${users}">
                    <form:option value="${theUser.userId.toString()}"><c:out value="${theUser.name} ${theUser.surname}"/></form:option>
                </c:forEach>
                </form:select>
            </td>
        </tr>
</form:form>

我的控制器:

@RequestMapping(value = "/admin/transferDevice/{deviceId}", method = RequestMethod.POST)
public String transferForDevice(@PathVariable("deviceId") int deviceId, @ModelAttribute("selectedUser") User user) throws Exception {
    //so now I can use "user" from @ModelAttribute
    return "redirect:/admin";
于 2013-07-17T13:53:40.533 回答
2

我不熟悉表单标签,它可以使用它吗?

@RequestMapping(value = "/admin/transferDevice/{deviceId}", method = RequestMethod.POST)
public String transferForDevice(@PathVariable("deviceId") int deviceId, @RequestParam("users") String user) throws Exception {

也许您的表单结束标签应该在 form:select 结束标签下。

于 2013-07-16T15:45:43.607 回答