首页 > 解决方案 > 为什么我收到此异常“Bean 名称 'orderModel' 的 BindingResult 和普通目标对象均不可用”;

问题描述

我正在使用 form:checkbox 之后我的表单给出了“Bean name 'orderModel' 的 BindingResult 和普通目标对象都不可用”。我将其更改为@ModelAttribute,因为我在搜索后找到了结果但没有奏效。

我的控制器类或 jsp 页面代码有什么问题,请检查我的代码。

输出

购物车.jsp

   <form:form action="/order"  method="POST" modelAttribute="orderModel">
    <table class="table table-striped table-hover table-bordered">
            <c:forEach items="${listCart}" var="items" varStatus="status">
                <tr>
                    <td><form:checkbox path="listCart[${status.index}].action" /></td>
                    <td style="text-align: center">${items.id}</td>
                    <td style="text-align: center">${items.name}</td>
                    <td style="text-align: center"><fmt:formatNumber type="currency">${items.price} 
         </fmt:formatNumber></td>
                    <td>
                </tr>
            </c:forEach>
        </table>
        <div class="col-md-5">
        <a class="btn btn-light btn-xl" href=" ">Order Now!</a>
            </div>
    </form:form>    

控制器类

@RequestMapping(value="/order", method=RequestMethod.POST)
public String createOrder(Model model) {

model.addAttribute("orderModel",new customerOrderModel());

 return "redirect:/view/cart/addItem";

   }

控制器类方法返回 cart.jsp

   @RequestMapping(value = "/order", method = RequestMethod.Post)
   public ModelAndView createOrder() {
    ModelAndView model = new ModelAndView();
   model.addObject("orderModel",new customerOrderModel());
   model.setViewName("Cart");
   return model;
}

customerOrderModel.class

 @Entity
  public class customerOrderModel {

@Id
@GeneratedValue
private String  OrderID;

@OneToOne
@JoinColumn(name="ID")
private CustomerModel customerID;

@OneToOne(cascade = CascadeType.ALL)
@JoinColumn(name = "billingAddressId")
private BillingAddress billingAddress;

@OneToOne(cascade = CascadeType.ALL)
@JoinColumn(name = "shippingAddressId")
private ShippingAddress shippingAddress;

@OneToOne(cascade = CascadeType.ALL)
@JoinColumn(name = "Cartid")
private Cart cart;

public String getOrderID() {
    return OrderID;
}

public void setOrderID(String orderID) {
    OrderID = orderID;
}

public CustomerModel getCustomerID() {
    return customerID;
}

public void setCustomerID(CustomerModel customerID) {
    this.customerID = customerID;
}

public BillingAddress getBillingAddress() {
    return billingAddress;
}

public void setBillingAddress(BillingAddress billingAddress) {
    this.billingAddress = billingAddress;
}

public ShippingAddress getShippingAddress() {
    return shippingAddress;
}

public void setShippingAddress(ShippingAddress shippingAddress) {
    this.shippingAddress = shippingAddress;
}

public Cart getCart() {
    return cart;
}

public void setCart(Cart cart) {
    this.cart = cart;
}
}

这是控制器类的最终代码

@RequestMapping("/order")
public ModelAndView createOrder() {
    customerOrderModel customerOrder = new customerOrderModel();
    Cart cart = cartdao.getCartByID(id);
    customerOrder.setCart(cart);
    CustomerModel customer = cart.getCustomer();
    customerOrder.setCustomerID(customer);
    customerOrder.setBillingAddress(customer.getBillingAddress());
    customerOrder.setShippingAddress(customer.getShippingAddress());
    orderDao.addCustomerOrder(customerOrder);
    return new ModelAndView("Cart","orderModel",new customerOrderModel());

标签: springjsp

解决方案


推荐阅读