Created
January 26, 2012 11:04
-
-
Save ksauzz/1682257 to your computer and use it in GitHub Desktop.
Spring MVC 3.x @Valid (JSR 303) Annotation Support
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter"> | |
| <property name="webBindingInitializer"> | |
| <!-- Configures Spring MVC DataBinder instances --> | |
| <bean class="org.springframework.web.bind.support.ConfigurableWebBindingInitializer"> | |
| <property name="validator" ref="validator" /> | |
| </bean> | |
| </property> | |
| </bean> | |
| <!-- Creates the JSR-303 Validator --> | |
| <bean id="validator" class="org.springframework.validation.beanvalidation.LocalValidatorFactoryBean" /> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import javax.validation.Valid; | |
| import javax.validation.constraints.NotNull; | |
| import org.springframework.validation.BindingResult; | |
| import org.springframework.beans.factory.annotation.Autowired; | |
| import org.springframework.beans.factory.annotation.Value; | |
| import org.springframework.stereotype.Controller; | |
| import org.springframework.ui.ModelMap; | |
| import org.springframework.web.bind.annotation.RequestMapping; | |
| import org.springframework.web.bind.annotation.RequestMethod; | |
| @Controller | |
| @RequestMapping("/hoge") | |
| public class Controller { | |
| public static class Bean { | |
| @NotNull | |
| private String name; | |
| @NotNull | |
| private String type; | |
| public void setName(String name) { | |
| this.name = name; | |
| } | |
| public String getName() { | |
| return Name; | |
| } | |
| public void setType(String type) { | |
| this.type = type; | |
| } | |
| public String getType() { | |
| return type; | |
| } | |
| } | |
| @RequestMapping(value = "foo", method=RequestMethod.POST) | |
| public String search(ModelMap model, @Valid Bean bean, BindingResult result) { | |
| if (result.hasErrors()) { | |
| // invalid | |
| return "error"; | |
| } | |
| // valid | |
| return "success"; | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <dependency> | |
| <groupId>org.hibernate</groupId> | |
| <artifactId>hibernate-validator</artifactId> | |
| <version>4.0.0.GA</version> | |
| </dependency> |
Author
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
see http://omgo.wordpress.com/2009/10/24/spring-mvc-3-x-valid-jsr-303-annotation-support/