首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Spring MVC @ModelAttribute获取填充实体

Spring MVC @ModelAttribute获取填充实体
EN

Stack Overflow用户
提问于 2012-12-17 22:12:49
回答 1查看 2K关注 0票数 0

今天我使用的是带有POST方法的spring-form,它没有给出我想要的控制器的POST项。这是我的代码。

Controller.java

代码语言:javascript
复制
@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

代码语言:javascript
复制
/// 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

代码语言:javascript
复制
public class SearchForm implements Serializable
{
   private Collection<Item> items;
   private String term;
   // getters and setters
}

${productList}是循环遍历所有项的backingbean。

我真的不知道问题是什么,为什么它没有给我通过POST传递的正确数据。非常感谢。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2012-12-17 22:26:23

将spring:hidden标签转换为普通的html隐藏标签:

代码语言:javascript
复制
<form:hidden path="items[${status.index}].id"/>

代码语言:javascript
复制
<input type="hidden" name="id" value="${item.id}"/>
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/13915790

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档