【问题标题】:I am getting an error while i am trying to add a product to the cart using Spring boot尝试使用 Spring Boot 将产品添加到购物车时出现错误
【发布时间】:2021-12-05 08:44:09
【问题描述】:

我希望用户插入客户信息并输入产品详细信息,一旦他单击添加,将在与此客户信息关联的购物车项目中创建一个产品。

主要问题:当我尝试选择一个类别时,它不会加载任何类别

类别实体

@Entity
@Data
public class Categories {

@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long categories_id;

private String categoryName;

我有产品、客户、订单实体。我需要找到关系以将所有这些实体收集在 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;
    }

产品实体类

    @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";
    }

订单实体

       @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>

【问题讨论】:

  • 你应该提供你得到的实际错误
  • 错误是什么?
  • 谢谢你们的时间,我刚刚更新了错误页面
  • 您访问的是*customer.customer_lname,它应该是*customer.customer_lName
  • thnx Popeye,是的,它有效,但现在我面临另一个问题,在显示类别、产品和价格中,所有这些都没有显示它包含的内容,就像我尝试选择类别时的类别一样,它不会加载或显示我之前创建的任何类别,因此我可以从中进行选择

标签: java spring spring-boot thymeleaf web-frontend


【解决方案1】:

您的 HTML 中有错字:

th:object="${CartItem}"

应该改为:

th:object="${cartItem}" 

因为你的控制器有:

@ModelAttribute("cartItem")

【讨论】:

  • thnx 注意到这样的事情,但它实际上并没有解决问题,我更新了这个问题。 thnx 再次,如果你看到任何变化,请 lmk
猜你喜欢
  • 2019-05-24
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-06-09
相关资源
最近更新 更多