2016-12-31 114 views
1

當驗證對象控制器只是刷新頁面,而不是通過窗體顯示錯誤:錯誤,你能告訴我問題在哪裏。我客人需要從結合的結果出現錯誤,並將其插入到頁面中,而不是控制器只是清爽頁面窗體:錯誤沒有顯示錯誤

控制器:

@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頁:

      <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

我認爲你的product模型屬性在y中有不同的名稱我們的POST-處理方法。

根據documentation

默認模型屬性名稱是從基於非限定類名聲明 屬性類型(即,方法的參數類型或方法返回類型), 推斷:例如「orderAddress」用於類 「mypackage.OrderAddress」或「orderAddressList」for 「List <mypackage.OrderAddress>」。

因此,您的ProductDto product屬性將在結果模型中具有名稱productDto。但是你在模板中指product

嘗試明確設置屬性@ModelAttributename註釋

public String createProduct(MultipartFile image, Model model, @ModelAttribute(name = "product") @Valid ProductDto product, BindingResult bindingResult) 

並確保bidning和確認實際工作,嘗試登錄它:

if (bindingResult.hasErrors()) { 
    System.err.println("Form has errors!"); 
}