我开始学习春季,我想创建一个电子商务商店。我在项目中使用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 但它仍会创建一个空元素。 有什么建议吗?