我想让用户插入客户信息并输入产品详细信息,一旦他点击添加,将在购物车中创建与此客户信息相关的产品。
主要问题是:当我尝试选择一个类别时,它不会加载任何类别
CategoryEntity
@Entity
@Data
public class Categories {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long categories_id;
private String categoryName;
我有Product、Customer、order实体。我需要找到将所有这些实体聚集在Order实体上的关系,该实体将保存客户信息和产品信息。
在这个Html页面中,我需要创建链接此信息,将被添加到订单实体,所以当我创建另一个页面来获取所有的订单,这样我就可以查看所有的订单与用户插入的信息。
创建一个账单HTML页面
客户实体类
@Entity
@Table(name="customers")
public class Customers {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long customer_id;
private String customer_fName;
private String customer_lName;
private String customer_email;
private String customer_address;
private String customer_state;
private String customer_phone;
private String customer_zipCode;
@OneToMany(targetEntity = Orders.class,cascade = CascadeType.ALL)
@JoinColumn(name = "customer_order_fk",referencedColumnName = "customer_id")//means will be a fk in orders table
private List<Orders> orders;
public Customers()
{
super();
}
public Customers(String customer_fName, String customer_lName, String customer_email, String customer_phone,String customer_address ,String customer_state,String customer_zipCode) {
this.customer_zipCode = customer_zipCode;
this.customer_phone = customer_phone;
this.customer_state = customer_state;
this.customer_address = customer_address;
this.customer_fName = customer_fName;
this.customer_lName = customer_lName;
this.customer_email = customer_email;
}
product实体类
@Entity
@Table(name = "Products")
@Data
public class Products {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private long product_id;
private String product_name;
private BigDecimal product_price;
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "categories_id",nullable = false) //the name of the column in the other class and that name will be a column in the class
private Categories product_category;
private String product_quantity;
private String product_Section;
private String product_ExpDate;
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "")
private Customers customer;
@ManyToOne
@JoinColumn(name = "order_order_id")
private Orders order;
public Products()
{
super();
}
public Products(String product_name, BigDecimal product_price,String product_quantity, String product_Section,String product_ExpDate) {
this.product_name = product_name;
this.product_price = product_price;
this.product_quantity = product_quantity;
this.product_Section = product_Section;
this.product_ExpDate = product_ExpDate;
}
CartItemsController
@Controller
public class CartItemsControllers {
@Autowired
private ShoppingCartImpService shoppingCartImpService;
//Model
@ModelAttribute("cartItem")
public CartItem cartItem()
{
return new CartItem();
}
//Curd
@GetMapping("/cart/create")
public String createCartItemForm(Model model)
{
//create order object
CartItem cartItem = new CartItem();
model.addAttribute("cartItem",cartItem);
return "makeABill";
}
//Save
@PostMapping("/cart/save")
public String saveCartItem(@ModelAttribute("cartItem") CartItem cartItem)
{
shoppingCartImpService.saveCart(cartItem);
return "MakeABill";
}
OrdersEntity
@Entity
@RequiredArgsConstructor
@AllArgsConstructor
@Data
public class Orders {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private long order_id;
@OneToMany(fetch = FetchType.LAZY,mappedBy = "cart_item_id",cascade = CascadeType.ALL)
private Set<CartItem> cartItem;
private double total_price;
}
MakeABill HTML页面
<div class="col-md-7 col-lg-8">
<h4 class="mb-3">Customer Information</h4>
<form class="needs-validation" novalidate="" th:action="@{/cart/save}" method="post" th:object="${cartItem}" id="form">
<div class="row g-3">
<div class="col-sm-6">
<label for="firstName" class="form-label">First name</label>
<input type="text" class="form-control" id="firstName" placeholder="" value="" required="" autofocus th:field="*{customer.customer_fName}">
<div class="invalid-feedback">
Valid first name is required.
</div>
</div>
<div class="col-sm-6">
<label for="lastName" class="form-label">Last name</label>
<input type="text" class="form-control" id="lastName" placeholder="" value="" required="" autofocus th:field="*{customer.customer_lName}">
<div class="invalid-feedback">
Valid last name is required.
</div>
</div>
<div class="col-12">
<br>
<label for="address" class="form-label">Address</label>
<input type="text" class="form-control" id="address" placeholder="Address" required="" autofocus th:field="*{customer.customer_address}">
<div class="invalid-feedback">
Please enter your shipping address.
</div>
</div>
</div>
<br>
<hr class="my-4">
<!--product info-->
<div class="row g-3">
<div class="col-12">
<h4 class="mb-3">Select A Product</h4>
<br>
<label th:for="category"> Category : </label>
<select class="form-control form-control-sm" id="category" name="category" autofocus>
<option value="">Select Category</option>
<option th:each = "product: ${cartItem}"
th:text="${product_category}"
>
</option>
</select>
<br>
<label th:for="product"> Product Name : </label>
<select class="form-control form-control-sm" id="product" name="product" autofocus>
<option value="">Select Product</option>
<option th:each = "product: ${cartItem}"
th:text="${product_name}"
>
</option>
</select>
<br>
<label th:for="product_price"> Product Price : </label>
<input class="form-control form-control-sm" id="product_price" name="product_price" disabled >
<br>
<label th:for="roles"> Product Quantity : </label>
<input class="form-control form-control-sm" id="product_Qty" name="product_Qty" autofocus>
<br>
<button class="w-5 btn btn-primary " type="submit" id="add_submit" >Add </button>
</div>
</div>
</form>
<br>
<hr class="my-4">
<!-- TABLE -->
<table class = "table table-striped table-bordered" id="show">
<thead class = "table-white">
<tr>
<th> Category </th>
<th> Product Name </th>
<th> Product Price </th>
<th> Product Quantity </th>
<th> Total </th>
<th> Edit </th>
<th> Delete </th>
</tr>
</thead>
<tbody>
<tr th:each = "product: ${product}"> <!-- this attribute to list up products -->
<td></td>
<td></td>
<td></td>
<td></td>
<td> <center> <a style="color: green"> Edit </a> </center> </td>
<td> <center> <a style="color: red"> Delete </a> </center> </td>
</tr>
</tbody>
</table>
<h4 class="mb-3"></h4>
<br>
<div class="row g-3">
<div class="col-12">
<h5 class="mb-3" id="total_bill"> Total: $</h5>
</div>
</div>
<br>
<button class="w-100 btn btn-primary btn-lg" type="submit">Generate Bill</button>
</div>
你的HTML中有打字错误:
th:object="${CartItem}"
应改为:
th:object="${cartItem}"
因为你的控制器有:
@ModelAttribute("cartItem")