Skip to content

Instantly share code, notes, and snippets.

@benelog
Created September 17, 2012 09:46
Show Gist options
  • Save benelog/3736481 to your computer and use it in GitHub Desktop.
Save benelog/3736481 to your computer and use it in GitHub Desktop.
Spring validation
@RequestMapping("/saveBoard")
public String saveBoard(@ModelAttribute @Valid Board board, BindingResult binding) {
if (binding.hasErrors()) {
return "boardInputForm";
}
Integer seq = service.save(board);
return "redirect:boardDetail/" + seq;
}
@InitBinder
protected void initBinder(WebDataBinder binder) {
binder.setValidator(new BoardValidator());
}
<dependency>
<groupId>javax.validation</groupId>
<artifactId>validation-api</artifactId>
<version>1.0.0.GA</version>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-validator-annotation-processor</artifactId>
<version>4.1.0.Final</version>
</dependency>
<div class="alert alert-error" th:if="${#fields.hasErrors('title')}" th:text="${#fields.errors('title')}">
title is empty
</div>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment