我想订购(购买)那些在购物车中选择的商品。为此,我使用了复选框,但混淆了如何按顺序传递它,现在是 href。
我想订购所有项目只需一次点击。
检查这个网址:/id=${items.id}“>现在命令!
cart.jsp
<form action="">
<c:forEach items="${listCart}" var="items">
<tr>
<td><input type="checkbox" name="chkbox" id="${items.id}"/></td>
<td style="text-align: center">${items.id}</td>
<td style="text-align: center">${items.name}</td>
</c:forEach>
<div class="row">
<div class="col-md-5">
<a class="btn btn-light btn-xl" href="<c:url value="/order"/>/id=${items.id}">Order Now!</a>
</div>
</div>
</div>
</form> controller.jsp
@ResponseBody
@RequestMapping(value="/order/{id}", method=RequestMethod.GET)
public String createOrder(@PathVariable int id) {
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 "redirect:/view/cart/addItem";
}发布于 2020-05-06 20:09:54
您是使用SpringMVC表单提交还是使用Ajax调用?
1) Option1 :使用SpringMVC表单向服务器提交。在本例中,遵循https://mkyong.com/spring-mvc/spring-mvc-form-handling-example/。您的表单需要指定其去向,以及如何将其字段映射到Data类(使用path)。您将使用SpringMVC表单标记。
<form:form method="post" modelAttribute="userForm" action="/order">
...
<form:input path="name" type="text" /> <!-- same for checkboxes or other controls -->
...
</form:form>2) Option2 :使用Ajax提交:在客户端使用JS获取所需的选择:
var selections = [];
// Find all selected checkboxes and store their values in an array
$('input[type="checkbox"]:checked').each(function(index) {
selections.push($(this).val());
});
// Now call Ajax and pass this array of selections to whatever receives it in SpringMVC...
$.ajax({
type : "post",
dataType : "json",
url : '/order',
data : JSON.stringify({'selections' : selections})
});https://stackoverflow.com/questions/61644107
复制相似问题