2017-09-07 36 views
1

BindingResultにエラーが表示されないが、スタックトレースに表示されます。私は、しようとした場合スプリングブートBindingResultでエラーが発生しない

@PostMapping("newSale") 
public String saleSubmit(@Valid @ModelAttribute("SaleViewModel") SaleViewModel saleViewModel, BindingResult result) { 
    if (result.hasErrors()) { 
     List<ObjectError> errors = result.getAllErrors(); 
     for(ObjectError error : errors) { 
      System.out.println("This is the error: " +error); 
     } 
     return "sale"; 
    } else { 
     // Other stuff 

:用途SalesViewDTO

public class SaleViewModel { 

private Sale sale = new Sale(); 

ためのHTMLとコントローラをバックアップDTO

@Entity 
public class Sale { 

public Sale() { 
} 

@Id 
@GeneratedValue(strategy = GenerationType.IDENTITY) 
private Long id; 
@NotEmpty 
@Pattern(regexp = "^S\\d{9}$", message = "Sales number must be in the format S123456789") 
private String salesno; 

私の売上高はsalesnoのための正規表現パターンを持っています私はコンソールでメッセージを得るフォームを提出する:

ConstraintViolationImpl{interpolatedMessage='Sales number must be in the format S123456789', propertyPath=salesno, rootBeanClass=class com.gmbh.domain.Sale, messageTemplate='Sale number must be in the format S123456789'} 

バインディング結果の結果が空である理由を知りたいのですが?

答えて

2

は、それは同様に、ネストされたオブジェクトを検証する必要がありますSaleViewModel

public class SaleViewModel { 
    @Valid 
    private Sale sale = new Sale(); 

の売却内部オブジェクトに@Validを追加します。

+0

完璧 - ありがとうございます! –

+0

私は別の質問を投稿しすぎる場合がありますが、どうやってhibernate @UniqueConstraintエラーをbindingresultに入れますか?com.mysql.jdbc.exceptions.jdbc4.MySQLIntegrityConstraintViolationException:重複したエントリ '123456789' –

+0

これは検証ロジックの一部ではありません。通常、カスタム注釈が導入される。 @UserNotExistsすべてのロジックがチェックされている場所http://dolszewski.com/spring/custom-validation-annotation-in-spring/ – StanislavL

関連する問題