Spring Boot更新嵌套对象

时间:2019-12-12 17:43:13

标签: spring-boot

我有一个用户,他(她)有一个项目列表。为了编辑项目数量,我将创建一个表单并列出用户的所有项目,每个项目的数量都在输入标签中。现在,当我编辑数量并提交时,我将发布一个带有已编辑信息的用户对象,然后将其保存到数据库中。但是,当我发布用户对象并通过@ModelAttribute("user")在控制器中获取它时,我得到了一个带有空项目列表的用户。我不知道我的代码在哪里错误,或者我不明白问题出在哪里。我是春季靴子的新手,将不胜感激。

非常感谢,并感谢我的英语。

我尝试过的代码:

控制器


import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.example.demo.entity.User;
import com.example.demo.service.user.UserService;

@Controller
@RequestMapping(path = "/user")
public class CartController {

    @Autowired
    UserService userService;

    @GetMapping("/cart/{id}")
    public String viewCart(Model model, @PathVariable("id") Integer id) {
        model.addAttribute("user", userService.findById(id).get());
        return "user/shopping_cart";
    }

    @GetMapping("/cart/checkout")
    public String checkout() {
        return "user/checkout";
    }

    @RequestMapping(value = "/cart/checkout", method = RequestMethod.POST)
    public String checkout(@ModelAttribute("user") User user) {
        //userService.save(user);
        System.out.println(user.getItems());
        return "redirect:/user/cart/checkout";
    }

}

查看

 <form th:action="@{/user/cart/checkout}" th:object="${user}" method="POST">          
       <div class="order-detail-content">
            <div class="table-responsive">
               <table class="table table-bordered cart_summary">
                 <thead>
                   <tr>
                     <th class="cart_product">Product</th>
                     <th>Description</th>
                      <th>Avail.</th>
                      <th>Unit price</th>
                      <th>Qty</th>
                      <th>Total</th>
                      <th  class="action"><i class="fa fa-trash-o"></i></th>
                    </tr>
                  </thead>
                    <tbody th:each="item: ${user.getItems()}">
                      <tr>
                       <td class="cart_product"><a href="#"><img th:src="'/images' + ${item.product.imageLink}" alt="Product"></a></td>
                        <td class="cart_description"><p class="product-name" th:text="${item.product.productName}"><a href="#"></a></p>
                          <small><a href="#">Color : Red</a></small><br>
                          <small><a href="#">Size : M</a></small></td>
                        <td class="availability in-stock"><span class="label">In stock</span></td>
                        <td class="price"><span th:text="${item.price}" th:id="'price' + ${item.product.code}"></span></td>
                        <td class="qty"><input class="form-control input-sm quantity" min="1" max="10" type="number" th:value="${item.quantity}" th:id="${item.product.code}"></td>
                        <td class="price"><span th:id="'subtotal'+${item.product.code}"></span></td>
                        <td class="action"><a href="#"><i class="icon-close"></i></a></td>
                      </tr>
                    </tbody>
                  <tfoot>
                    <tr>
                      <td colspan="2" rowspan="2"></td>
                      <td colspan="3">Total products (tax incl.)</td>
                      <td colspan="2" id="total1"></td>
                    </tr>
                    <tr>
                      <td colspan="3"><strong>Total</strong></td>
                      <td colspan="2"><strong id="total2"></strong></td>
                    </tr>
                  </tfoot>
                </table>
              </div>
              <div class="cart_navigation"> 
                <a class="continue-btn" href="#"><i class="fa fa-arrow-left"> </i>&nbsp; Continue shopping</a>
                <button type="submit" class="checkout-btn" href="#"><i class="fa fa-check"></i> Proceed to checkout</button>
              </div>
            </div>
          </form> ```


0 个答案:

没有答案