本文介绍了在Spring MVC中,ModelAttribute在控制器中返回空值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好,是时候寻求帮助了;我正在向我的jsp发送一个(购物)购物车ModelAttribute,允许用户编辑数量,当Model是POST到控制器时,除可编辑(数量)字段外,这些字段为null.我已经针对类似的问题进行了数天的研究,但没有匹配的结果.我正在使用Spring 3.1.

这是我在GET和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";
      }

和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";
   }

我的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>

以及之前在GET上的购物车的日志结果:

,然后将jsp中的数量从1更新为3,然后发布到控制器:

我已经在这里和Spring论坛上阅读了几篇类似的文章,并尝试了不同的建议解决方案,但都没有碰到运气.看来我编辑的数量结果正确地绑定到对象,但是为什么其他不呢?

解决方案

假定您在Form对象中具有所有必需的字段;

您必须指定表单字段并用数据填充值.

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

仅将结果打印到屏幕上.如果要将其绑定到表单,则必须将其放入spring表单输入中,例如:

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

如果您不希望它是可编辑的,则可以将其放入隐藏字段中,但是最后您必须将其放入jsp的form元素中,并在表单POJO中具有相应的字段

Ok, its time to seek help; I am sending a (shopping) Cart ModelAttribute to my jsp, allowing the user to edit the quantity, when the Model is POST to the controller the fields are null except the editable (quantity) field. I have researched for days on similar issues but nothing is matching. I am using spring 3.1.

Here is my controller on the 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 the log results for cart before on GET:

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

I've read several similar post here and on the Spring forum and tried different suggested solutions with no luck. It seems like my edited quantity results are getting bound to the Object correctly but why aren’t the others?

解决方案

Assuming you have all the necessary fields in your Form object;

You have to specify the form fields and fill the value with your data.

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

will only print the result to the screen. If you want to bind it to your form you have to put it in a spring form input such as:

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

If you don't want it to be editable then you can put it into a hidden field but in the end you'll have to put it in a form element in the jsp and have a corresponding field in your form POJO

这篇关于在Spring MVC中,ModelAttribute在控制器中返回空值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-22 09:34