我有这个模型:

Order.java

@Entity
@Table(name = "`order`")
public class Order {
    private Long id;
    @OneToMany(fetch = FetchType.EAGER, mappedBy = "order")
    private List<OrderProduct> orderProducts;

    @Override
    public int hashCode() {
        return new Long(id).hashCode();
    }

    @Override
    public boolean equals(Object obj) {
        if (obj == null) {
            return false;
        }
        if (!(obj instanceof Order)) {
            return false;
        }
        return this.id == ((Order) obj).getId();
    }

    /* getters & setters */
}


OrderProduct.java

@Entity
@Table(name = "order_product")
public class OrderProduct {
    private Long id;
    private String name;
    private Long quantity;
    @ManyToOne
    @JoinColumn(name = "order_id")
    private Order order;

    /* getters & setters */
}


而这个控制器:

OrderController.java

@Controller
@SessionAttributes({"products"})
public class OrderController {
    @Autowired
    private OrderService orderService;

    @Autowired
    private ProductService productService;

    @Autowired
    private SecurityService securityService;

    @Autowired
    private UserService userService;

    @RequestMapping(value = "/order/add", method = RequestMethod.GET)
    public String addOrder(Model model) {
        Order order = new Order();
        order.setOrderProducts(new AutoPopulatingList<>(OrderProduct.class));
        model.addAttribute("orderForm", order);
        model.addAttribute("products", productService.findAll());
        return "order/add";
    }

    @RequestMapping(value = "/order/add", method = RequestMethod.POST)
    public String addOrder(@ModelAttribute("orderForm") Order orderForm, BindingResult bindingResult, Model model) {
        orderForm.setUser(userService.findByUsername(securityService.findLoggedInUsername()));
        for (OrderProduct orderProduct : orderForm.getOrderProducts()) {
            orderProduct.setOrder(orderForm);
        }
        orderService.save(orderForm);
        return "redirect:/order/view";
    }
}


我想创建一个表单,用户可以在其中选择少量产品,提供一定数量的产品并提交订单。我试图写这样的JSP代码:

<form:form method="POST" modelAttribute="orderForm">
    <spring:bind path="orderProducts">
        <tr>
            <td>
                <c:forEach items="${products}" var="product">
                    <form:checkbox path="orderProducts[${product.id}].name"
                        value="${product.name}"
                        label="${product.name}"/>
                    <form:input type="text" path="orderProducts[${product.id}].quantity" placeholder="Quantity"/>
                </c:forEach>
            </td>
        </tr>
    </spring:bind>
    <button class="btn btn-lg btn-primary btn-block" type="submit">Submit</button>
</form:form>


...但是不幸的是,它可能试图创建与我在产品列表中一样多的orderProducts,因此服务尝试保存的orderProducts没有正确设置的值,最后创建的订单没有orderProducts。因此,我的问题是如何通过JSP代码正确地将orderProducts发送到控制器,该控制器最终将使用其物料创建正确的订单?

最佳答案

我怀疑这个$ {product.id}。由于这是一个索引值,因此应该像
orderProducts [0]。名称orderProducts [0] .quantity
orderProducts [1]。名称orderProducts [1] .quantity
orderProducts [2]。名称orderProducts [2] .quantity

10-06 03:43