@RequestMapping(value = "/admin/productInventory/editProduct/{productId}")
public String editProduct(@PathVariable("productId") String productId,Model model){
Product product = productDao.getProductById(productId);
model.addAttribute(product);
return "editProduct";
}
@RequestMapping(value ="/admin/productInventory/editProduct",method = RequestMethod.POST)
public String editProduct(@ModelAttribute("product") Product product,HttpServletRequest request){
MultipartFile productImage = product.getProductImage();
String rootDirectory = request.getSession().getServletContext().getRealPath("/");
Path path = Paths.get(rootDirectory + "\\WEB-INF\\resources\\images\\" + product.getProductId() + ".png");
if(productImage !=null && !productImage.isEmpty()){
try{
productImage.transferTo(new File(path.toString()));
}catch(Exception e){
throw new RuntimeException("product image failed",e);
}
}
productDao.editProduct(product);
return "redirect:/admin/productInventory";
}
<div class="form-group">
<label for="name">Name</label>
<form:input path="productName" id="name" class="form-control"/>
</div>
<div class="form-group">
<label for="Category">Category</label>
<label class="checkbox-inline"><form:radiobutton path="productCategory" id="category"
value="Instrument"/>Instrument</label>
<label class="checkbox-inline"><form:radiobutton path="productCategory" id="category"
value="Record"/>Record</label>
<label class="checkbox-inline"><form:radiobutton path="productCategory" id="category"
value="accessory"/>Accessory</label>
</div>
<div class="form-group">
<label for="description">Description</label>
<form:input path="productDescription" id="description" class="form-control" value="${product.productDescription}"/>
</div>
<div class="form-group">
<label for="name">price</label>
<form:input path="productPrice" id="price" class="form-control" value="${product.productName}"/>
</div>
<div class="form-group">
<label for="condition">Condition</label>
<label class="checkbox-inline"><form:radiobutton path="productCondition" id="condition"
value="New"/>New</label>
<label class="checkbox-inline"><form:radiobutton path="productCondition" id="condition"
value="Used"/>Used</label>
</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="unitInStock">Unit in stock</label>
<form:input path="unitInStock" id="unitInStock" class="form-control" value="${product.unitInStock}"/>
</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="button btn-default">Cancel</a>
</form:form>
公共类产品{
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private String productId;
private String productName;
private String productCategory;
private String productDescription;
private double productPrice;
private String productCondition;
private String productStatus;
private int unitInStock;
private String productManufacturer;
@Transient
private MultipartFile productImage;
public String getProductName() {
return productName;
}
public String getProductId() {
return productId;
}
public void setProductId(String productId) {
this.productId = productId;
}
public void setProductName(String productName) {
this.productName = productName;
}
public String getProductCategory() {
return productCategory;
}
public void setProductCategory(String productCategory) {
this.productCategory = productCategory;
}
public String getProductDescription() {
return productDescription;
}
public void setProductDescription(String productDescription) {
this.productDescription = productDescription;
}
public double getProductPrice() {
return productPrice;
}
public void setProductPrice(double productPrice) {
this.productPrice = productPrice;
}
public String getProductCondition() {
return productCondition;
}
public void setProductCondition(String productCondition) {
this.productCondition = productCondition;
}
public String getProductStatus() {
return productStatus;
}
public void setProductStatus(String productStatus) {
this.productStatus = productStatus;
}
public int getUnitInStock() {
return unitInStock;
}
public void setUnitInStock(int unitInStock) {
this.unitInStock = unitInStock;
}
public String getProductManufacturer() {
return productManufacturer;
}
public void setProductManufacturer(String productManufacturer) {
this.productManufacturer = productManufacturer;
}
public MultipartFile getProductImage() {
return productImage;
}
public void setProductImage(MultipartFile productImage) {
this.productImage = productImage;
}
}
<mvc:annotation-driven/>
<bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/views/" />
<property name="suffix" value=".jsp"/>
</bean>
<mvc:resources mapping="/resources/**" location="/WEB-INF/resources/"/>
<tx:annotation-driven/>
<bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
<property name="dataSource" ref="dataSource"></property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.H2Dialect</prop>
<prop key="hibernate.hbm2ddl.auto">update</prop>
<prop key="hibernate.show_sql">true</prop>
<prop key="hibernate.format_sql">true</prop>
</props>
</property>
<property name="packagesToScan">
<list>
<value>com.emusicstore</value>
</list>
</property>
</bean>
<bean id="transactionManager" class="org.springframework.orm.hibernate4.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory"/>
</bean>
<bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
<property name="maxUploadSize" value="10000024" />
</bean>
我得到400错误我不知道我做错了但是例如如果我删除ModelAttribute然后它的工作可以有人帮助我在这里出错了。如果我创建新的控制器,例如然后邮件工作,但没有它我试图检查与Firefox的调试器,我看到一个错误的请求。
答案 0 :(得分:0)
在MVC
图层中使用实体作为模型是不好的做法,尝试使用单独的模型。
您在此处输入错误产品价格为double
,无法将其分配给字符串。
<div class="form-group">
<label for="name">price</label>
<form:input path="productPrice" id="price" class="form-control" value="${product.productName}"/>
</div>
将其更改为:
<div class="form-group">
<label for="name">price</label>
<form:input path="productPrice" id="price" class="form-control" value="${product.productPrice}"/>
</div>
并且避免使用primitives
作为模型属性,否则您将在视图中使用无用的默认值,如果您将输入设置为空,则会出现错误,因为primitives
没有默认null
值。