jsp - ModelAttribute returns null values in controller in Spring MVC -


ok, time seek help; sending (shopping) cart modelattribute jsp, allowing user edit quantity, when model post controller fields null except editable (quantity) field. have researched days on similar issues nothing matching. using spring 3.1.

here controller on , 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 log results cart before on get:

cartcontroller.showcart() cart: cart [cartdetails=[cartdetail product=com.product@c26440[name=my name], quantity=1]], totalcartprice=10.00]

and after updating quantity 1 3 in jsp , post controller:

cartcontroller.update() cart: cart [cartdetails=[cartdetail [product=null, quantity=3]], totalcartprice=null]

i've read several similar post here , on spring forum , tried different suggested solutions no luck. seems edited quantity results getting bound object correctly why aren’t others?

assuming have necessary fields in form object;

you have specify form fields , fill value data.

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

will print result screen. if want bind form have put in spring form input such as:

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

if don't want editable can put hidden field in end you'll have put in form element in jsp , have corresponding field in form pojo


Comments

Popular posts from this blog

javascript - RequestAnimationFrame not working when exiting fullscreen switching space on Safari -

jsf - How to ajax update an item in the footer of a PrimeFaces dataTable? -

django - CSRF verification failed. Request aborted. CSRF cookie not set -