spring - 为什么我收到此异常“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());
解决方案
推荐阅读
- c - C 加速递归
- r - R GGplot - County choropleth map - 如何在没有数据的情况下勾勒出州/县?
- json - 递归父子 JSON Postgresql
- gitlab - Gitlab:书签回购功能 - 如何以及什么?
- c# - ASP .NET 在表单请求后错误地将十进制数转换为数百
- android - 为 android sdk 30 读取存储卡上的自定义文件
- firebase - 如何使用生产存储桶数据启动 Firebase 存储模拟器?
- python-3.x - 通过 python3 计算 uniswap 3.0 池(对)地址
- java - Kotlin - 如何根据条件返回对象
- kotlin - 什么时候是块,什么时候是 lambda?