Spring MVC-从JSP提交对象
问题内容:
我有一个JSP,可以显示客户列表(ArrayList searchResults)。我希望能够选择其中之一,并将其提交给Spring
MVC控制器。但是,看来我无法传递所选对象,只能传递其属性,例如customerId。我真的需要传递整个对象。
在Spring 3.x中,有没有标准的方法可以做到这一点?
<c:forEach items="${searchResults}" var="searchResult">
<tr>
<td><c:out value="${searchResult.customerId}" /></td>
<td><c:out value="${searchResult.firstName}" /></td>
<td><c:out value="${searchResult.lastName}" /></td>
<td>
<form method="POST" ACTION="./customercare">
<input type="SUBMIT" value="Select This Customer"/>
<input type="hidden" name ="searchResult" value="${searchResult}"/>
</form>
</td>
</tr>
</c:forEach>
问题答案:
您可以使用Spring的form
taglib而不是plain
<form>
来将其回发到Spring MVC Controller,然后它将值绑定回您指定的模型。
<form:form method="post" action="addContact.html">
<table>
<tr>
<td><form:label path="firstname">First Name</form:label></td>
<td><form:input path="firstname" /></td>
</tr>
…
@RequestMapping(value = "/addContact", method = RequestMethod.POST)
public String addContact(@ModelAttribute("contact")
Contact contact, BindingResult result) {
看到这个帖子:http :
//viralpatel.net/blogs/spring-3-mvc-handling-
forms/