Spring provides many ways to validate an object, I have posted an article about Spring's Validator Interface, now I will show you how to validate an object with JSR 303 in Spring 3.x by using Hibernate validator.
This is an implementation of JSR 303
For more details about validation annotations, have a look at Hibernate Validator,
A JSR-303 provider, such as Hibernate Validator, is expected to be present in the classpath and will be detected automatically.
Normally, the error message must follow [Constraint].[Bean name].[Property name]
For example, the application.properties contains:
References:
1. Import Hibernate Validator 4.x to project
<dependency> <groupId>org.hibernate</groupId> <artifactId>hibernate-validator</artifactId> <version>4.3.1.Final</version> </dependency>
This is an implementation of JSR 303
2. Validation annotation
There is a few difference with the validation by using Spring's Validator Interface. We define the constraints in object such properties:public class User { @NotEmpty(message="First Name is required") private String firstName; @NotEmpty private String lastName; @CodeValidator private String code; @NotEmpty private String password; @NotEmpty private String repassword; .... }
For more details about validation annotations, have a look at Hibernate Validator,
3. Support validation JSR 303
To support JSR 303 in Spring application we modify the *-servlet.xml to add<mvc:annotation-driven/>or
<bean id="validator" class="org.springframework.validation.beanvalidation.LocalValidatorFactoryBean"/>
A JSR-303 provider, such as Hibernate Validator, is expected to be present in the classpath and will be detected automatically.
4. Controller
In controller, when you want to validate an object, you put the @Valid annotation in front of its place, the framework will detect and make validation for us.@RequestMapping(value = "/modifyUser", method = RequestMethod.POST) public ModelAndView updateUser(@Valid @ModelAttribute("user") User user, BindingResult bindingResult) { if (bindingResult.hasErrors()) { // show errors return new ModelAndView("modifyUser", "user", user); } else { // success return new ModelAndView("viewUser", "user", user); } }
5. Configure ResourceBundle
- Create new properties file in folder src/main/resources
- Create new bean instance in *-servlet.xml to load resource file
<bean id="messageSource"
class="org.springframework.context.support.ResourceBundleMessageSource">
<property name="basename" value="application" />
</bean>
Normally, the error message must follow [Constraint].[Bean name].[Property name]
For example, the application.properties contains:
NotEmpty.user.firstName = First Name is required
NotEmpty.user.lastName = Last Name is required
NotEmpty.user.password = Password is required
NotEmpty.user.repassword = Retype password is required
6. Prepare JSP page
Do not have anything new in JSR 303 for JSP, you do it as well as Spring's Validator Interface<tr> <td><form:label path="firstName">First Name</form:label></td> <td> <form:input path="firstName" /> <form:errors path="firstName" class="errors" /> </td> </tr>
References:
Thanks for the post, it is really helpful.
ReplyDeleteI was wondering how validation can be done in case we have @OneToMany mapping (say Employee class has Set of Address and none of the address field is inputted by the user from frontend, thus returning NULL to Spring MVC)?
Thanks in advance,