Вы находитесь на странице: 1из 2

Spring Validators.

When we accept user inputs in any web application, it become necessary to validate them. We
can validate the user input at client side using JavaScript but its also necessary to validate them
at server side to make sure we are processing valid data in case user has JavaScript disabled.
Spring MVC Framework supports JSR-303 specs by default and all we need is to add JSR-303 and
its implementation dependencies in Spring MVC application. Spring Framework provides a
ready-to-use, simple validation framework out of the box through which we can get the errors
raised by Validator implementation in the controller request handler method. Spring validation
provides a simplified set of APIs and supporting classes for validating domain objects. As similar
to other spring elements, the validation framework is extensible and it is possible to hook-in
custom validator implementations into the framework.
Spring's data binding and validation framework is simple but functional - all you need to do is
implement the Validator interface. In particular, if you use Spring's MVC framework (and its
command/form controllers) then you're probably using Spring validation as well. If not, you
should be! Moving your validation outside the controller into a Validator implementation allows
you to declaratively assign and reuse your validation logic.

Link to the Spring Validator Docshttp://docs.spring.io/spring/docs/3.0.x/api/org/springframework/validation/packagesummary.html


Link to the ValidationUtils Docshttp://docs.spring.io/spring/docs/3.0.x/api/org/springframework/validation/ValidationUtils.ht
ml

Spring Validation is the combination of Validator interface and DataBinder or in other words
spring achieve validation with the help of these two. Validator interface can be used to validate
an Object, which (validator interface) in turn uses Error Object to report any validator error
while validating an Object.
Validator (org.springframework.validation.Validator) interface has two main methods.
1. supports(Class): Class(Instance of) this validator supports to.

2. validate(Object, org.springframework.validation.Errors) : Validate the Object and reports


the validation error via Error Object

<!-- Form Validation using Annotations -->


<dependency>
<groupId>javax.validation</groupId>
<artifactId>validation-api</artifactId>
<version>1.1.0.Final</version>
</dependency>
<!-- Spring -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>${spring.core.version}</version>
<exclusions>
<!-- Exclude Commons Logging in favour of SLF4j -->
<exclusion>
<groupId>commons-logging</groupId>
<artifactId>commons-logging</artifactId>
</exclusion>
</exclusions>
</dependency>

Dependencies Needed

Вам также может понравиться