Hibernate validator of Spring MVC JSR-303 validation framework

For JSR 303 validation, there are currently two implementations, one is Hibernate Validator and the other is Apache BVal. This tutorial uses Hibernate Validator. Note that it has nothing to do with Hibernate, but only uses it for data verification.

1. Download and install Hibernate Validator

Users can use the address“ https://sourceforge.net/projects/hibernate/files/hibernate-validator/ ”Download Hibernate Validator. Hibernate-validator-4.3.2 is selected for this tutorial Final-dist.zip.

First unzip the downloaded compressed package, and then unzip \ hibernate-validator-4.3.2 Hibernate-validator-4.3.2. In the final \ dist directory Final. Jar and \ hibernate-validator-4.3.2 Jboss-logging-3.1.0 in the final \ dist \ lib \ required directory Final. jar,validation-api-1.0.0. Copy ga.jar to the \ WEB-INF\lib directory of the application.

2. Configure property file and verifier

If you put the validation error message in the property file, you need to configure the property file in the configuration file and associate the property file with Hibernate Validator. The specific configuration code is as follows:

<!-- Configure message properties file -->
<bean id="messageSource" class="org.springframework.context.support.ReloadableResourceBundleMessageSource">
    <!-- Resource file name -->
    <property name="basenames">
    <!-- Resource file coding format -->
    <property name="fileEncodings" value="utf-8" />
    <!-- The time to cache the contents of the resource file, in seconds -->
    <property name="cacheSeconds" value="120" />
<!-- Register verifier -->
<bean id="validator" class="org.springframework.validation.beanvalidation.LocalValidatorFactoryBean">
    <!-- hibernate Calibrator -->
    <property name="providerClass" value="org.hibernate.validator.HibernateValidator" />
    <!-- Specify the resource file used for verification, and configure the verification error information in the file. If it is not specified, it will be used by default classpath Lower ValidationMessages.properties -->
    <property name="validationMessageSource" ref="messageSource" />
<!--open Spring of Valid function -->
<mvc:annotation-driven conversion-service="conversionService" validator="validator" />

3. Marking type

JSR 303 does not need to write a verifier, but needs to use its annotation type to embed constraints on the attributes of the domain model.

1) Empty check

  • @Null: verify that the object is null.
  • @NotNull: verify that the object is not null, unable to check the string with length 0.
  • @NotBlank: check whether the constraint string is null and whether the length after being trim med is greater than 0. It is only for strings, and the front and back spaces will be removed.
  • @NotEmpty: check whether the constraint element is null or empty.

Examples are as follows:

@NotBlank(message="{goods.gname.required}") //goods.gname.required is the error code of the property file
private String gname;

2) boolean check

  • @AssertTrue: verify whether the boolean property is true.
  • @AssertFalse: verify whether the boolean property is false.

Examples are as follows:

private boolean isLogin;

3) Length check

  • @Size (min =, max =): verify whether the length of the object (Array, Collection, Map, String) is within the given range.
  • @Length (min =, max =): verify whether the length of the string is within the given range.

Examples are as follows:

private String gdescription;

4) Date check

  • @Past: verify that the Date and Callendar objects are before the current time.
  • @Future: verify whether the Date and Calendar objects are after the current time.
  • @Pattern: verify whether the String object conforms to the rules of regular expressions.

Examples are as follows:

private Date gdate;

5) Numerical check

Examples are as follows:

private double gprice;

Due to the limited space, you can learn from the tutorial spring MVC hibernate validator data validation example.

Added by phpdoodler on Sat, 19 Feb 2022 19:37:17 +0200