验证对象控制器时只刷新页面,而不是通过表单显示错误:错误,你能告诉我问题出在哪里。我访客需要从绑定结果中获取错误并将其插入页面,而控制器只是刷新页面
控制器:
@PreAuthorize("hasRole('ROLE_ADMIN')")
@RequestMapping(value = "/create", method = RequestMethod.POST)
public String createProduct(MultipartFile image, Model model,@ModelAttribute @Valid ProductDto product, BindingResult bindingResult) throws IOException {
productValidator.validate(product,bindingResult);
if (bindingResult.hasErrors()){
return "admin/create";
} else {
return "index";
}
}
@PreAuthorize("hasRole('ROLE_ADMIN')")
@RequestMapping(value = "/create", method = RequestMethod.GET)
public String createProduct(Model model) {
model.addAttribute("product", new ProductDto());
model.addAttribute("categories", categoryService.findAll());
return "admin/create";
}
验证者:
@Override
public boolean supports(Class<?> aClass) {
return ProductDto.class.equals(aClass);
}
@Override
public void validate(Object o, Errors errors) {
ProductDto product = (ProductDto) o;
if (product.getTitle().isEmpty() || product.getTitle() == null) {
errors.rejectValue("title", "product.title", "Product title cant be empty");
}
if (product.getDescription().isEmpty() || product.getDescription() == null) {
errors.rejectValue("description", "product.description", "Product description cant be empty");
}
if (product.getPrice() == null || product.getPrice()<=0) {
errors.rejectValue("price", "product.price", "Product price is not valid");
}
if (product.getCategoryId()==null) {
errors.rejectValue("category", "product.category", "Product category is not valid");
}
}
jstl page:
<spring:url value="/product/create" var="formUrl"/>
<form:form modelAttribute="product" action="${formUrl }" method="post" enctype="multipart/form-data">
<div class="form-group">
<label>Title</label>
<form:input id="title"
cssClass="form-control" path="title"/>
<form:errors path="title"/>
</div>
<div class="form-group">
<label>Description</label>
<form:textarea id="description" rows="10"
cssClass="form-control" path="description"/>
</div>
<div class="form-group">
<label>Price</label>
<form:input id="price" type="number"
cssClass="form-control" path="price"/>
<form:errors path="description"/>
</div>
<div class="form-group">
<label for="sel1">Select category</label>
<form:select id="sel1" cssClass="form-control" path="categoryId">
<form:options items="${categories}" itemValue="id" itemLabel="title"/>
</form:select>
</div>
<label class="btn btn-default btn-file">
Image <input type="file" multiple accept='image/*' ng-file-select="onFileSelect($files)" name="image" style="display: block;">
</label>
<br><br>
<div class="text-center">
<button type="submit" class="btn btn-lg btn-success text-center"><span
class="fa fa-check"></span> Submit
</button>
<a href="/" class="btn btn-danger btn-lg text-center"><span
class="fa fa-times"></span> Cancel
</a>
</div>
</form:form>
答案 0 :(得分:0)
我认为您的product
模型属性在POST
处理方法中有不同的名称。
默认模型属性名称是从声明的推断出来的 属性类型(即方法参数类型或方法返回类型), 基于不合格的类名:例如类的“orderAddress” “mypackage.OrderAddress”或“orderAddressList”表示 “列表与LT; mypackage.OrderAddress&gt;” 中。
因此,您的ProductDto product
属性在结果模型中将具有名称productDto
。但您在模板中引用了product
。
尝试明确设置name
注释
@ModelAttribute
属性
public String createProduct(MultipartFile image, Model model, @ModelAttribute(name = "product") @Valid ProductDto product, BindingResult bindingResult)
要确保出价和验证确实有效,请尝试记录:
if (bindingResult.hasErrors()) {
System.err.println("Form has errors!");
}