ModelAttribute returns null values ​​in the controller in Spring MVC

Well, its time to seek help; I send the (Shop) Cart ModelAttribute to my jsp, allowing the user to edit the quantity, when the model is POST for the controller, the fields are zero, except for the editable (quantity) field. I explored the days on similar issues, but nothing matches. I am using spring 3.1.

Here is my controller on GET and POST:

@Controller
public class CartController {

      @Autowired
      private Cart cart;        

      @RequestMapping(value = "/cart", method = RequestMethod.GET)  
      public String showCart(Model model) {
         logger.debug("CartController.showCart() Cart: {}", this.cart);
         model.addAttribute(cart);
         return "cart/cart";
      }

and post

   @RequestMapping(value = "/cart", method = RequestMethod.POST, params = "update")
   public String update(@ModelAttribute("cart") Cart cart, BindingResult result, Model model) {
      logger.debug("CartController.update() Cart: {}", cart); 
      return "cart/cart";
   }

my jsp:

<div class="container MainContent">
   <form:form method="POST" modelAttribute="cart">
      <fieldset>
         <legend>Cart</legend>
         <table class="table">
            <thead>
               <tr>
                  <th>Product Name</th>
                  <th>Quantity</th>
                  <th>Product Price</th>
               </tr>
            </thead>
            <tbody>
               <c:forEach items="${cart.cartDetails}" var="cartDetail" varStatus="status">
                  <tr>
                     <td>${cartDetail.product.name}</td>                     
                     <td><form:input path="cartDetails[${status.index}].quantity" size="1" /></td>                     
                     <td>${cartDetail.price}</td>
               </c:forEach>
               <tr>
                  <b><td colspan="2" align="right"><spring:message code="order.total" /></b>
                  </td>
                  <td>${cart.totalCartPrice}</td>
               </tr>
            </tbody>
         </table>
      </fieldset>
      <div></div>
      <button id="order" name="order">
         <spring:message code="button.order" />
      </button>
      <button id="update" name="update">
         <spring:message code="button.update" />
      </button>
   </form:form>
</div>

and magazine results for carts before GET:

CartController.showCart () Cart: Cart [cartDetails = [CartDetail product=com.Product@c26440 [name = My Name], quantity = 1]], totalCartPrice = 10.00]

and after updating the quantity from 1 to 3 in jsp, and then POST to the controller:

CartController.update() : [cartDetails = [CartDetail [product = null, quantity = 3]], totalCartPrice = null]

spring . , , ?

+4
3

, Form;

.

<td>${cartDetail.product.name}</td> 

. , spring, :

<form:input path="productName"  value="${cartDetail.product.name}"/> 

, , , jsp POJO

+4

, , , ,

<td>${cartDetail.product.name}
<form:hidden path="cartDetails[${status.index}].product.name" value="${cartDetail.product.name}"/></td>
+2

. , Binder:

@InitBinder
void initBinder(final WebDataBinder binder) {
    binder.setAllowedFields("name", ...);
}

, . , null.

: Bean, @ModelAttribute. , Object setName(String name) void setName(String).

0

All Articles