【发布时间】:2020-07-28 00:10:42
【问题描述】:
我目前正在编写一个电子商务网络应用程序,并且正在开发添加到购物车功能。
当您添加到购物车时,它会创建一个发布请求,将数据提交到用户的购物车。我想知道是否有更好的编写方法(如何将表单数据放在 URL 中)。
当有人点击添加到购物车时,它应该根据当前登录的用户(带有 spring 安全性)在这种类型的 url 中发送一个发布请求: /cart/{productId}/{quantity} - 然后它将被添加到用户的购物车中。
百里香代码:
<div th:each="product : ${popularProducts}">
<form action="#" th:action="@{/cart/} + ${product.getId() + @{/} + ${quantity}" th:object="${product}" method="POST">
<div class="card">
<img class="card-img" th:src="${product.getPictureUrl()}">
<a href="#" class="card-link text-danger like">
<i class="fas fa-heart"></i>
</a>
</div>
<div class="card-body">
<h4 class="card-title" th:text="${product.getName()}"></h4>
<h6 class="card-subtitle mb-2 text-muted" th:text="${product.getCategory()}"></h6>
<p class="card-text" th:text="${product.getDescription()}">
<div class="buy d-flex justify-content-between align-items-center">
<div class="price text-success"><h5 class="mt-4" th:text="'$' + ${product.getPrice()}"></h5></div>
<div class="form-group blu-margin">
<div class="form-label-group">
<input th:field="*{quantity}" type="text" id="quantity" name="quantity" class="form-control" placeholder="1" required="required">
</div>
</div>
<a href="#" class="btn btn-danger mt-3"><i class="fas fa-shopping-cart"></i> Add to Cart</a>
</div>
</div>
</form>
购物车控制器:
@PostMapping("/cart/{productId}/{quantity}")
public void addProduct(@PathVariable String productId, @PathVariable String quantity, Model model) {
Object principal = SecurityContextHolder.getContext().getAuthentication().getPrincipal();
User user = ((User) principal);
if (productService.findById(Long.parseLong(productId)) != null) {
Optional<Product> product = productService.findById(Long.parseLong(productId));
int quantityAmount = Integer.parseInt(quantity);
user.getCart().getCartItems().add(new CartItems(product.get(), quantityAmount,
product.get().getPrice() * quantityAmount, user.getCart()));
}
List<CartItems> itemsInCart = user.getCart().getCartItems();
int numItems = itemsInCart.size() + 1;
model.addAttribute("amountOfItems", numItems);
}
产品类别:
@Table(name = "products")
public class Product {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "product_id")
private Long id;
@Column(name = "name")
private String name;
@Column(name = "category")
private String category;
@Column(name = "description")
private String description;
@Column(name = "price")
private Double price;
@Column(name = "stock")
private int stock;
@Column(name = "picture_url")
private String pictureUrl;
public Product() {
}
public Product(String name, String category, String description, Double price, int stock, String pictureUrl) {
this.name = name;
this.category = category;
this.description = description;
this.price = price;
this.stock = stock;
this.pictureUrl = pictureUrl;
}
// and getters/setters...
CartItems 类:
@Table(name = "cartitems")
public class CartItems implements Serializable {
private static final long serialVersionUID = -3426230490106082532L;
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private int id;
@Column(name = "quantity")
private int quantity;
@Column(name = "price")
private double price;
@OneToOne
@JoinColumn(name = "product_id")
private Product product;
@ManyToOne
@JoinColumn(name = "cart_id")
private Cart cart;
public CartItems() {
}
public CartItems(Product product, int quantity, double price, Cart cart) {
this.quantity = quantity;
this.price = price;
this.product = product;
this.cart = cart;
}
// all getters and setters..
【问题讨论】:
标签: spring-boot spring-mvc spring-security thymeleaf