Я начал изучать весну и хочу создать интернет-магазин.Я использую Spring + MySQL для моего проекта.Пока мне удалось реализовать Create, Read и Delete, но Update не будет работать правильно и честно, я не знаю, что делать ...
Вот мой код:
HomeController.class
@RequestMapping("/admin/productInventory/editProduct/{id}")
public String editProduct(@PathVariable("id") Long id, Model model) {
Product product = productDAO.getProductByID(id);
model.addAttribute(product);
return "editProduct";
}
@RequestMapping(value = "/admin/productInventory/editProduct", method = RequestMethod.POST)
public String editProductPost(@ModelAttribute("product") Product product) {
productDAO.editProduct(product);
return "redirect:/admin/productInventory";
}
Редактировать функцию продукта в ProductDAOImplementation.class
public void editProduct(Product product) {
Session session = sessionFactory.getCurrentSession();
session.saveOrUpdate(product);
session.flush();
}
А вот мой editProduct.jsp
<%@taglib prefix="form" uri="http://www.springframework.org/tags/form" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@include file="/WEB-INF/view/template/header.jsp" %>
<div class="container-wrapper">
<div class="container">
<div class="page-header">
<h1>Edit Product</h1>
<p class="lead">Please update the product information here:</p>
</div>
<form:form action="${pageContext.request.contextPath}/admin/productInventory/editProduct" method="post"
commandName="product" enctype="multipart/form-data">
<form:hidden path="productID" value="${product.productID}" />
<div class="form-group">
<label for="name">Name:</label>
<form:input path="productName" id="name" class="form-Control" value="${product.productName}"/>
</div>
<div class="form-group">
<label for="category">Category:</label>
<label class="checkbox-inline"><form:radiobutton path="productCategory" id="category"
value="sneakers" />Sneakers</label>
<label class="checkbox-inline"><form:radiobutton path="productCategory" id="category"
value="shoes" />Shoes</label>
<label class="checkbox-inline"><form:radiobutton path="productCategory" id="category"
value="boots" />Boots</label>
</div>
<div class="form-group">
<label for="description">Description</label>
<form:textarea path="productDescription" id="description" class="form-Control" value="${product.productDescription}"/>
</div>
<div class="form-group">
<label for="category"></label>
<label class="checkbox-inline"><form:radiobutton path="productSize" id="size"
value="40" />40</label>
<label class="checkbox-inline"><form:radiobutton path="productSize" id="size"
value="41" />41</label>
<label class="checkbox-inline"><form:radiobutton path="productSize" id="size"
value="42" />42</label
<label class="checkbox-inline"><form:radiobutton path="productSize" id="size"
value="43" />43</label>
<label class="checkbox-inline"><form:radiobutton path="productSize" id="size"
value="44" />44</label>
</div>
<div class="form-group">
<label for="price">Price</label>
<form:input path="productPrice" id="price" class="form-Control" value="${product.productPrice}"/>
</div>
<div class="form-group">
<label for="status">Status</label>
<label class="checkbox-inline"><form:radiobutton path="productStatus" id="status"
value="active" />Active</label>
<label class="checkbox-inline"><form:radiobutton path="productStatus" id="status"
value="inactive" />Inactive</label>
</div>
<div class="form-group">
<label for="quantity">Quantity</label>
<form:input path="productQuantity" id="quantity" class="form-Control" value="${product.productQuantity}"/>
</div>
<div class="form-group">
<label for="manufacturer">Manufacturer</label>
<form:input path="productManufacturer" id="manufacturer" class="form-Control" value="${product.productManufacturer}"/>
</div>
<br><br>
<input type="submit" value="Submit" class="btn btn-default">
<a href="<c:url value="/admin/productInventory" />" class="btn btn-default">Cancel</a>
</form:form>
<%@include file="/WEB-INF/view/template/footer.jsp" %>
Кто-то сказал, что я должен добавить скрытый путь, чтобы получитьтекущий productID, но он все еще создает пустой элемент.Любой совет?