Spring 3.0 supports the new JSR-303 validation. JSR-303 standardizes validation constraint declaration and metadata for the Java platform. The following model object has its fields annotated with JSR-303 (javax.validation.*).
@Entity(name = "partner")
@Indexed
public class Partner extends NamedEntity implements Serializable {
@Column
@javax.validation.constraints.NotNull(message = "partnerType must not be null.")
private PartnerType partnerType;
@javax.validation.NotEmpty(message = "Lanaguage can't be empty")
private String language;
@javax.validation.NotEmpty(message = "contact person can't be empty")
private String contactPerson;
}
Spring 3.0 reference guide.
explains how to configure and use JSR-303 in Spring 3.0 , but there is no clear way of doing samething in Spring Webflow.
Model validation is needed whenever you want to bind a model with data from a form submitted by a user and with JSR-303, anytime you want to valide an bean class. In this post, I am going to explain how you can configure JSR-303 validator factory in spring context as a bean and use it in both Spring MVC and Spring Webflow.
Add the following bean in your mvc application context.
<bean id="validator" class="org.springframework.validation.beanvalidation.LocalValidatorFactoryBean" />
You could also get the default validator factory in controller or validator class.
private Validator validator= Validation.buildDefaultValidatorFactory().getValidator();
- Spring MVC
In mvc, there are several ways that you can use the above validator in your web controller:
Assuming that you using annotation driven mvc/controller.
<!-- Dispatches requests mapped to POJO @Controllers implementations -->
<bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter">
<property name="webBindingInitializer">
<bean class="org.springframework.web.bind.support.ConfigurableWebBindingInitializer">
<property name="validator" ref="validator" />
</bean>
</property>
</bean>
if you are using the new mvc schema namespace.
<mvc:annotation-driven conversion-service="conversionService" validator="validator"/>
you can also dependency inject the validator to your controller
@Autowired private javax.validation.Validator validator
To activate the validation in your controller, you can either add @Valid annotion in the respected method as a parameter before the model object. This will atutomaticly validate the model object, and update BindingResult with constraint messages.
@RequestMapping(value = "/partner/", method = RequestMethod.POST)
public String createPartner(@ModelAttribute("partner") @Valid Partner partner, BindingResult result) {
Assert.notNull(partner, "Partner must be provided.");
if (result.hasErrors())
return "partner_create";
partnerService.persist(partner);
return "redirect:/partner/" + partner.getId();
}
or manually activate the validation against the model.
@RequestMapping(value = "/partner/", method = RequestMethod.POST)
public String createPartner(@ModelAttribute("partner") Partner partner, BindingResult result) {
Assert.notNull(partner, "Partner must be provided.");
for (ConstraintViolation<Partner> constraint : validator.validate(partner)) {
result.rejectValue(constraint.getPropertyPath().toString(), "",
constraint.getMessage()); }
if (result.hasErrors())
return "partner_create";
partnerService.persist(partner);
return "redirect:/partner/" + partner.getId();
]
The above finishes what you need to do to utilize the new JSR-303 in spring MVC, next I will show you how to acheive samething in your webflow.