Spring MVC - Validation

1) Validation with Spring MVC validator


From Spring 3, Spring mvc has the ability to validate @Controller inputs automatically.
Before Spring 3, Developer used to invoke validation logic manually but  from Spring 3  no need to call the validation logic manually.

Below are the ways to configure the Validation in Spring 3 mvc.

  •  You can set validator by calling binder.setValidator(Validator) within a @Controller's @InitBinder callback and annotate the input argument as @Valid.

  @Controller
  public class YourController {

  @InitBinder
  protected void initBinder(WebDataBinder binder) {
   binder.setValidator(new LoginFormValidator());
  }
    
  @RequestMapping(value = "/login",method = RequestMethod.POST)
  public String submitForm(@Valid LoginForm  loginForm, BindingResult result,ModelMap model) {
  
  if (result.hasErrors()) {
         return "loginForm";
     }
  
  model.addAttribute("loginForm",loginForm);
  return "success";
 
 }
 
 }

OR

  •  You may call setValidator(Validator) on the global WebBindingInitializer.

This allows you to configure a Validator instance across all @Controllers. This can be achieved easily    by using the Spring MVC namespace as Below
 <beans xmlns="http://www.springframework.org/schema/beans"
 xmlns:context="http://www.springframework.org/schema/context"
 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xmlns:mvc="http://www.springframework.org/schema/mvc"
 xsi:schemaLocation="
        http://www.springframework.org/schema/beans     
        http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
        http://www.springframework.org/schema/context 
        http://www.springframework.org/schema/context/spring-context-3.0.xsd
        http://www.springframework.org/schema/mvc
        http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd">

 <context:component-scan base-package="com.controller" />
  <mvc:annotation-driven validator="loginFormValidator"/>
  
</beans>
Complete Example on Validation with Spring MVC validator

2) Validation with JSR-303 Validator

Hibernate Validator is the reference implementation for JSR-303 annotation-drive validation. By using this JSR-303 we can validate  bean with simple annotations.

The Spring MVC configuration required to enable JSR-303 support is shown below:
 
 <?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:mvc="http://www.springframework.org/schema/mvc"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="
        http://www.springframework.org/schema/beans 
        http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
        http://www.springframework.org/schema/mvc
        http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd">

    <!-- JSR-303 support will be detected on classpath and enabled automatically -->
    <mvc:annotation-driven/>
    
</beans>
With this minimal configuration, anytime if any a @Valid @Controller input is encountered, it will be validated by the JSR-303 provider.
Simple example on JSR-303 Validator

1 comment:

  1. A precise, quick and to the point explaination of Spring validations. Good article.

    ReplyDelete

Related Posts Plugin for WordPress, Blogger...