与JSP表单中的多个属性的OneToMany关系

时间:2017-01-08 21:16:41

标签: java forms hibernate jsp spring-mvc

我有这个型号:

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正确发送到控制器,最终将使用其项目创建正确的订单?

2 个答案:

答案 0 :(得分:0)

我怀疑这是$ {product.id}。由于这是一个指数值,它应该像是 orderProducts [0] .name orderProducts [0] .quantity
orderProducts [1] .name orderProducts [1] .quantity
orderProducts [2] .name orderProducts [2] .quantity

答案 1 :(得分:0)

我通过从orderForm中删除orderProducts来解决这个问题,该订单生成器没有在我的OrderController中正确设置数量但可能不是最好的解决方案。