今天我使用的是带有POST方法的spring-form,它没有给出我想要的控制器的POST项。这是我的代码。
Controller.java
@Controller
@RequestMapping("/cart")
public class CartController extends CommonController
{
@RequestMapping(value = "/add", method = RequestMethod.POST)
public ModelAndView addCart(@ModelAttribute("productList") Item item, BindingResult result,Model model){
System.out.println(item.getId()); /// <-- doesn't gives me the ID
return new ModelAndView("cart");
}
}ProductList.jsp
/// Loop through the products of search itemlist and generates the forms with the correct items
<c:forEach var="item" items="${productList.items}" varStatus="status">
${item.name}
<div class="addCart">
<c:url value="/cart/add.html" var="addURL" />
<form:form method="POST" action="${addURL}" modelAttribute="productList">
<form:hidden path="items[${status.index}].id"/>
<input type="submit" class="addCartBtn" value="Add to cart" />
</form:form>
</div>
BackingBean.java
public class SearchForm implements Serializable
{
private Collection<Item> items;
private String term;
// getters and setters
}${productList}是循环遍历所有项的backingbean。
我真的不知道问题是什么,为什么它没有给我通过POST传递的正确数据。非常感谢。
发布于 2012-12-17 22:26:23
将spring:hidden标签转换为普通的html隐藏标签:
<form:hidden path="items[${status.index}].id"/>至
<input type="hidden" name="id" value="${item.id}"/>https://stackoverflow.com/questions/13915790
复制相似问题