簡體   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