项目作者: Md-MamunAbdulKayum

项目描述 :
Spring MVC custom validation
高级语言: Java
项目地址: git://github.com/Md-MamunAbdulKayum/Spring-Custom-Validation.git


Spring-Custom-Validation

Here I come with a project on “Spring MVC Custom validation”. Let’s see what we got here:

Author :

Md Mamun Abdul Kayum

Technologies :

  1. Spring MVC, Bootstrap 3.x

Required dependency for custom validation purpose:

  1. <dependency>
  2. <groupId>javax</groupId>
  3. <artifactId>javaee-web-api</artifactId>
  4. <version>7.0</version>
  5. <scope>provided</scope>
  6. </dependency>

Tested on Application Servers :

Tomcat 8, Glassfish 4

Build Tool:

Maven

Description:

For achieving spring mvc custom validation:

  1. you have to add “form:errors” tad at the form input field like:

    1. <div class="form-group">
    2. <label class="control-label col-sm-2" for="name">Customer Name:</label>
    3. <div class="col-sm-10">
    4. <form:input path="name" value="" type="text" class="form-control" placeholder="Customer Name"></form:input>
    5. <font color="red"><form:errors path="name"></form:errors></font>
    6. </div>
    7. </div>

    Add this error tag to those fields you want to validate.

  2. You must a propertise file(Say message.properties) which will contains your validation messages. In our case it is “messages.properties” file located at webapp/WEB-INF/i18n/messages.properties
    It contains our messages:

    ```

    validation message for product

    NotEmpty.productName=enter a name
    NotEmpty.quality=enter a quality
    NotEmpty.measureUnit=enter a measure unit
    NotEmpty.quantity=enter a quantity

    validation message for customer

    NotEmpty.name=enter a name
    NotEmpty.education=enter your education
    NotEmpty.address=enter your address
    NotEmpty.designation=enter a designation

Range.quantity=quantity range should be between 1 - 100
Range.age=age should be greater than 5

  1. 3. Now you have to add a validator class for our form validation, it contains validation logics:

if (target instanceof CustomerForm) {
CustomerForm customerForm = (CustomerForm) target;
if (!StringUtils.hasText(customerForm.getName())) {
errors.rejectValue(“name”, “NotEmpty.name”);
}
if (!StringUtils.hasText(customerForm.getEducation())) {
errors.rejectValue(“education”, “NotEmpty.education”);
}
if (!StringUtils.hasText(customerForm.getAddress())) {
errors.rejectValue(“address”, “NotEmpty.address”);
}
if (!StringUtils.hasText(customerForm.getDesignation())) {
errors.rejectValue(“designation”, “NotEmpty.designation”);
}
if (customerForm.getAge() < 5) {
errors.rejectValue(“age”, “Range.age”);
}
}

  1. From here we are sending errors if a filed failed to a validation logic.
  2. 4. Now from Controller class you have to check if a filed have any validation error:<br>
  1. CustomValidator userValidator = new CustomValidator();
  2. userValidator.validate(customerObject, result);
  3. if (result.hasErrors()) {
  4. return "addCustomer";
  5. }
  6. ```
  7. <br>

What we will get here:

addcustomer

addproduct