带有Spring-Boot和Thymeleaf的基本购物车实施
我正在尝试使用Spring-Boot和Thymeleaf进行非常基本的购物车实施。我在表中的页面上显示了产品。用户选择使用与该产品相对应的复选框的产品,并提供每个数字输入的数量。按下提交按钮后,我需要创建一个订单形式对象,该对象具有称为条目的字段,即订购对象的列表。有一个订购对象存储每个选择产品的条形码和数量。
我的代码的相关部分如下。
products.html
<form class="ui form" th:action="@{/products}" method="post" th:object="${orderForm}">
<ul>
<tr th:each="product, ind : ${productDTO.products}">
<input hidden name="barcode" th:value="${product.getBarcode()}"/>
<td th:text="${product.barcode}"/>
<td th:text="${product.name}"/>
<td th:text="${product.description}"/>
<td>
<input type="number" min="0" value="0" name="quantity">
</td>
<td>
<div class="ui checkbox">
<input type="checkbox" name="bool" th:value="${product.barcode}" th:field="${orderForm.entries}"/>
</div>
</td>
</tr>
</ul>
<th>
<div class="field">
<button class="ui button" type="submit" >Checkout</button>
</div>
</th>
</form>
orderformconverter.java
public class OrderFormConverter implements Converter<String, OrderFormEntry> {
@Override
public OrderFormEntry convert(String id) {
Long parseID = Long.parseLong(id);
List<Product> products = Arrays.asList(
new Product("ice tea",1L, "tea"),
new Product("caramel latte",2L, "coffee"),
new Product("vanilla latte",3L, "coffee")
);
List<OrderFormEntry> entries = Arrays.asList(
new OrderFormEntry(products.get(0).getBarcode(),0), //second parameter should be the quantity of the product
new OrderFormEntry(products.get(1).getBarcode(),0),
new OrderFormEntry(products.get(2).getBarcode(),0)
);
int index = (int) (parseID -1);
return entries.get(index);
}
}
controller.java
@GetMapping("/products")
public String products(Model model) {
List<Product> products = new ArrayList<>();
PS.findAll().iterator().forEachRemaining(products::add);
model.addAttribute("productDTO", new ProductDTO(products));
OrderForm orderForm = new OrderForm();
model.addAttribute("orderForm", orderForm);
return "products";
}
@PostMapping("/products")
public String add(@ModelAttribute OrderForm orderForm, @RequestParam(value="quantity") int quantity,
@RequestParam(value="barcode") Long barcode) {
//I am not sure about this part
return "success";
}
i可以使用转换器创建对象,但是使用此方法,我无法设置订单formentry对象的数量字段。由于我需要手动提供产品,我也不确定转换器部件,是否有更好的方法来执行此操作?
I am trying to do a very basic shopping cart implementation with spring-boot and thymeleaf. I displayed products on the page in a table. The user chooses products with the checkbox corresponding to that product and provides the quantity of each as number input. After submit button is pressed, I need to create an OrderForm object that has a field called entries, which is a List of OrderFormEntry objects. There is an OrderFormEntry object storing the barcode and quantity of each chosen product.
The relevant parts of my code are the following.
products.html
<form class="ui form" th:action="@{/products}" method="post" th:object="${orderForm}">
<ul>
<tr th:each="product, ind : ${productDTO.products}">
<input hidden name="barcode" th:value="${product.getBarcode()}"/>
<td th:text="${product.barcode}"/>
<td th:text="${product.name}"/>
<td th:text="${product.description}"/>
<td>
<input type="number" min="0" value="0" name="quantity">
</td>
<td>
<div class="ui checkbox">
<input type="checkbox" name="bool" th:value="${product.barcode}" th:field="${orderForm.entries}"/>
</div>
</td>
</tr>
</ul>
<th>
<div class="field">
<button class="ui button" type="submit" >Checkout</button>
</div>
</th>
</form>
OrderFormConverter.java
public class OrderFormConverter implements Converter<String, OrderFormEntry> {
@Override
public OrderFormEntry convert(String id) {
Long parseID = Long.parseLong(id);
List<Product> products = Arrays.asList(
new Product("ice tea",1L, "tea"),
new Product("caramel latte",2L, "coffee"),
new Product("vanilla latte",3L, "coffee")
);
List<OrderFormEntry> entries = Arrays.asList(
new OrderFormEntry(products.get(0).getBarcode(),0), //second parameter should be the quantity of the product
new OrderFormEntry(products.get(1).getBarcode(),0),
new OrderFormEntry(products.get(2).getBarcode(),0)
);
int index = (int) (parseID -1);
return entries.get(index);
}
}
Controller.java
@GetMapping("/products")
public String products(Model model) {
List<Product> products = new ArrayList<>();
PS.findAll().iterator().forEachRemaining(products::add);
model.addAttribute("productDTO", new ProductDTO(products));
OrderForm orderForm = new OrderForm();
model.addAttribute("orderForm", orderForm);
return "products";
}
@PostMapping("/products")
public String add(@ModelAttribute OrderForm orderForm, @RequestParam(value="quantity") int quantity,
@RequestParam(value="barcode") Long barcode) {
//I am not sure about this part
return "success";
}
I can create the objects with the converter just fine, but with this method I could not set the quantity field of OrderFormEntry objects. I am also not very sure about the converter part since i need to provide the products manually, is there a better way to do this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论