繁体   English   中英

Spring MVC @ModelAttribute获得实体

[英]Spring MVC @ModelAttribute getting filled Entity

今天我用POST方法坚持使用spring-form,它不会将发布的项目提供给我想要的Controller。 这是我的代码。

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传递的正确数据。 非常感谢。

隐藏你的弹簧:隐藏标签到正常的html隐藏标签:

<form:hidden path="items[${status.index}].id"/>

<input type="hidden" name="id" value="${item.id}"/>

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM