我的代码有一个奇怪的错误。我正在为电子商店编写代码。
我在管理员添加名称字段为空的产品时编写了验证代码。当我单击提交按钮时,addProduct页面将重新加载,并显示我定义的消息错误。
以类似的方式,我在管理员编辑产品时编写验证代码。我尝试编辑一个空名称的产品,该名称违反了我在Product.java中定义的NotEmpty。
HomeController的AddProduct
@RequestMapping(value = "/admin/productInventory/addProduct", method = RequestMethod.POST)
public String addProductPost(@Valid @ModelAttribute("product") Product product, BindingResult result,
HttpServletRequest request) {
if (result.hasErrors()) {
return "addProduct";
}
addProduct.jsp
<div class="form-group">
<label for="name">Name</label>
<form:errors path="productName" cssStyle="color: #ff0000;" />
<form:input path="productName" id="name" class="form-Control" />
</div>
HomeController的EditProduct
@RequestMapping(value = "/admin/productInventory/editProduct", method = RequestMethod.POST)
public String editProduct(@Valid @ModelAttribute("product") Product product, Model model, BindingResult result,
HttpServletRequest request) {
if (result.hasErrors()) {
return "editProduct";
}
editProduct.jsp
<div class="form-group">
<label for="name">Name</label>
<form:errors path="productName" cssStyle="color: #ff0000;" />
<form:input path="productName" id="name" class="form-Control"
value="${product.productName}" />
</div>
product.java
@Column(name="productName")
@NotEmpty (message = "The product name must not be null.")
private String productName;
当我尝试单击editProduct上的提交按钮(带有空名称)时,我选择:
HTTP Status 400 -
type Status report
message
description The request sent by the client was syntactically incorrect.
Apache Tomcat/9.0.0.M17
出了什么问题?
修改
editProduct.jsp
<%@taglib prefix="form" uri="http://www.springframework.org/tags/form"%>
<%@include file="/WEB-INF/views/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:errors path="productName" cssStyle="color: #ff0000;" />
<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="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:textarea path="productDescription" id="description"
class="form-Control" value="${product.productDescription}" />
</div>
<div class="form-group">
<label for="price">Price</label>
<%-- <form:errors path="productPrice" cssStyle="color: #ff0000;" /> --%>
<form:input path="productPrice" id="price" class="form-Control"
value="${product.productPrice}" />
</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:errors path="unitInStock" cssStyle="color: #ff0000;" /> --%>
<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="productManufacture" id="manufacturer"
class="form-Control" value="${product.productManufacture}" />
</div>
<div class="form-group">
<label class="control-label" for="productImage">Upload
Picture</label>
<form:input id="productImage" path="productImage" type="file"
class="form:input-large" />
</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>
</div>
</div>
<%@include file="/WEB-INF/views/template/footer.jsp"%>
header.jsp中
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<html>
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">
<!-- The above 3 meta tags *must* come first in the head; any other head content must come *after* these tags -->
<meta name="description" content="">
<meta name="author" content="">
<link rel="icon" href="../../favicon.ico">
<title>My Music Store</title>
<!-- Bootstrap core CSS -->
<link href="<c:url value="/resources/css/bootstrap.min.css" />"
rel="stylesheet">
<!-- Carousel core CSS -->
<link href="<c:url value="/resources/css/carousel.css" />"
rel="stylesheet">
<!-- My Main CSS style-->
<link href="<c:url value="/resources/css/main.css" />" rel="stylesheet">
</head>
<!-- NAVBAR
================================================== -->
<body>
<div class="navbar-wrapper">
<div class="container">
<nav class="navbar navbar-inverse navbar-static-top">
<div class="container">
<div class="navbar-header">
<button type="button" class="navbar-toggle collapsed"
data-toggle="collapse" data-target="#navbar" aria-expanded="false"
aria-controls="navbar">
<span class="sr-only">Toggle navigation</span> <span
class="icon-bar"></span> <span class="icon-bar"></span> <span
class="icon-bar"></span>
</button>
<a class="navbar-brand" href="#">My Music Store</a>
</div>
<div id="navbar" class="navbar-collapse collapse">
<ul class="nav navbar-nav">
<li><a href="<c:url value="/" />">Home</a></li>
<li><a href="<c:url value="/productList" />">Products</a></li>
<li><a href="#contact">Contact</a></li>
</ul>
<ul class="nav navbar-nav pull-right">
<li><a href="<c:url value="/admin/" />">Admin</a></li>
</ul>
</div>
</div>
</nav>
</div>
</div>
再次编辑
产品
package com.entities;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Table;
import javax.persistence.Transient;
import javax.validation.constraints.Min;
import org.hibernate.validator.constraints.NotEmpty;
import org.springframework.web.multipart.MultipartFile;
@Entity
@Table(name = "product")
public class Product {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(name="productId")
private String productId;
@Column(name="productName")
@NotEmpty (message = "The product name must not be null.")
private String productName;
@Column(name="productCategory")
private String productCategory;
@Column(name="productDescription")
private String productDescription;
@Min (value = 0, message = "The product price must no be less than zero.")
@Column(name="productPrice")
private double productPrice;
@Column(name="productCondition")
private String productCondition;
@Column(name="productStatus")
private String productStatus;
@Min (value = 0, message = "The product unit must no be less than zero.")
@Column(name="unitInStock")
private int unitInStock;
@Column(name="productManufacture")
private String productManufacture;
@Transient /*do not store to database*/
private MultipartFile productImage;
public String getProductId() {
return productId;
}
public void setProductId(String productId) {
this.productId = productId;
}
public String getProductName() {
return productName;
}
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 getProductManufacture() {
return productManufacture;
}
public void setProductManufacture(String productManufacture) {
this.productManufacture = productManufacture;
}
public MultipartFile getProductImage() {
return productImage;
}
public void setProductImage(MultipartFile productImage) {
this.productImage = productImage;
}
}