Verification method

  • Add the jar package
  • Configure the Validator in springMVC.xml
  • validationMessageSource.properties
  • Specify validation rules in the POJO
  • The checksum binding is used in controller
  • The JSP page

A case in field

Springmvc itself has no validation functionality, it uses Hibernate’s validation framework, which is unrelated to ORM

Add the jar package

Configure the Validator in springMVC.xml

Basically just copy it and use it.

Configure the following, equivalent to someone help us write the verification code, we take it and use it directly, so we need to configure.

Register the Validator with the adapter

Method 1 (Recommended)

Method 2: If the configuration file uses an adapter written in a non-annotated manner, do this

Springmvc.xml configures the validator

    <! -- Configure validator -->
    <bean id="validator" class="org.springframework.validation.beanvalidation.LocalValidatorFactoryBean">
        <property name="providerClass" value="org.hibernate.validator.HibernateValidator"></property>
        <property name="validationMessageSource" ref="validationMessageSource"></property>
    </bean>
    
    <! Configure validationMessageSource -->
    <bean id="validationMessageSource" class="org.springframework.context.support.ReloadableResourceBundleMessageSource">
        <! -- Specifies the base file name of the resource file for the validation information, excluding the suffix, which defaults to properties -->
        <property name="basenames">
            <list>
                <value>classpath:validationMessageSource</value>
            </list>
        </property>
        <! -- Specify the file encoding -->
        <property name="fileEncodings" value="utf8"></property>
        <! Caches the content of the resource file in seconds
        <property name="cacheSeconds" value="120"></property>
    </bean>
Copy the code

validationMessageSource.properties

The configuration file is used to store the text message when the verification fails, that is, it is extracted and stored in the configuration file

Specify validation rules in the POJO

List two validation rules (using annotation validation), notnull and size

1, items. The name. The size and the items. Createtime) notnull: is reading validationMessageSource. The properties of configuration information. From here, you can understand the meaning of the configuration file and prevent hard coding.

2. Use annotations to bind the properties that need to be validated, and you can make these annotations work only after configuring the previous steps.

The checksum binding is used in controller

1. The @ validation function takes effect on the annotated data verification rule (@notnull, etc.) in the POJO. Without the annotation declaration, the annotated data verification rule in the POJO will not take effect

2. The BindingResult object is used to obtain the information about the Validated verification failure (message in @notnull). The BindingResult object and the @validated annotation must be used together

3, The logic in this code should be easy to understand, which is to extract all error information from result, and then display the error information in the original page. Note that to use model object, we need to declare model model in the parameter, and then use

The JSP page

Extended to packet verification

What is packet check?

The verification rules are made in the POJO, and the same POjo can be used by multiple controllers. In this case, there is a problem: Different Controller methods check the same POJO, and the checksum information is shared between the different Controller methods, but each Controller method may actually require different checksum, in which case group checksum is used to solve this problem. Generally speaking, a POJO has many properties. Method 1 in controller may only need to check property 1 in POJO, and method 2 in controller may only need to check property 2 in POJO. However, there are many verification annotations in POJO. How about method two only checks property two? I’m going to have to do a packet check.

Define the group

The interface class is used only as this grouping identifier. See the following usage to see what it means

Using the grouping

The controller method

In this method, only ValidationGroup1 group annotations in the Items POJO are validated, and no others are validated