Validation parameter verification

This is the back-end small class of the monastery. Each article is shared from

[background introduction] [knowledge analysis] [common problems] [solutions] [coding practice] [extended thinking] [more discussion] [References]

Eight aspects of in-depth analysis of back-end knowledge / skills. This article shares:

[validation parameter verification]

[Java class of the Academy] validation parameter verification

Hello, I'm Zhang Quanliang, a student of Zhengzhou branch of it Academy. I'm an honest, pure and kind java programmer. Today I'd like to share with you,

Java task 2 on the official website of the Academy, knowledge points in deep thinking -- validation parameter verification

1. Background:

Parameter verification is a common problem. Whether in the front end or background, user input needs to be verified to ensure the correctness of system data.

For the web, some people may naturally want to verify on the front end, but this is a very wrong approach. The front-end code is transparent to users,

People with a little technology can bypass this verification and submit data directly to the background. Parameter validation can be seen everywhere and is essential.

The front-end verification is only for the user experience, and the back-end is the ultimate guarantee.

2. Knowledge analysis:

(1) Common parameter inspection methods:

a. Presentation layer verification: Spring MVC provides presentation layer verification of jsr-303, or directly use if to judge the obtained parameters in the controller layer

b. Business logic layer validation: spring 3 1. Provide method verification for business logic layer

c. Dao layer verification: Hibernate provides verification of Dao layer model data

d. Database side validation: through database constraints

The principle is based on javax validation. If there are special needs, you can customize the validation

(2) JSR 303

JSR 303 (Java specification requests specification proposal) is a sub specification in Java EE 6,

A set of criteria for JavaBean parameter verification is called bean validation.

JSR 303 is used to verify the value of a field in a java bean, spring MVC 3 Jsr303 is also strongly supported after X

The verification annotation provided by jsr303 can be annotated on the fields and properties of JavaBeans, or on maps and lists. The annotation is very flexible

@Null the annotated element must be null

@Notnull the annotated element must not be null

@Asserttrue the annotated element must be true

@Assertfalse the annotated element must be false

@Min (value) the annotated element must be a number and its value must be greater than or equal to the specified minimum value

@Max (value) the annotated element must be a number whose value must be less than or equal to the specified maximum value

@Decimalmin (value) the annotated element must be a number whose value must be greater than or equal to the specified minimum value

@Decimalmax (value) the annotated element must be a number whose value must be less than or equal to the specified maximum value

@Size (max =, min =) the size of the annotated element must be within the specified range

@Digits (integer, fraction) the annotated element must be a number and its value must be within an acceptable range

@Past annotated element must be a past date

@Future the annotated element must be a future date

@Pattern (regex =, flag =) the annotated element must conform to the specified regular expression

(3) Hibernate Validator

Hibernate validator is a reference implementation of jsr-303. In addition to supporting all standard verification annotations, it also supports the following extension annotations

@The annotated element of email must be an email address

@Length (min =, max =) the size of the annotated string must be within the specified range

@Notblank validation string is not null and must be longer than 0

@Notempty the of the annotated string must be non empty

@Range (min =, max =) the annotated element must be within the appropriate range

(4) Spring verification framework

Spring's org springframework. Validation is the package where the validation framework is located. The localvalidatorfactorybean implements both

The validator interface of spring implements the validator interface of jsr-303. Just define a localvalidatorfactorybean in the spring container

You can inject it into the bean that needs data verification.

3. Frequently asked questions:

(1) The difference between spring validation and javax validation

JSR 303 is a standard that specifies some verification specifications, namely verification annotations, which are located in javax validation. Constraints package Only specifications are provided, not implementations

Hibernate validation, which provides the corresponding implementation and adds some other verification annotations

Spring validation adds automatic verification to the spring MVC module and encapsulates the verification information into specific classes

(2) Basic use of parameter verification

Verification of multiple attributes

Output verification information in the log or console

Some annotations can only be used for some data types

If the @ notblank annotation is only used for strings, you should pay attention when using it

(3) Custom message error message

Spring defines a MessageSource interface to support the internationalization of information and the replacement of information containing parameters.

The ApplicationContext interface inherits the MessageSource interface,

Therefore, all ApplicationContext implementation classes implement the MessageSource interface.

That is, we can call the MessageSource interface method through ApplicationContext to internationalize the information and replace the parameters contained in the information.

4. Coding practice:

5. Expand thinking:

(1) Group check

Grouping is simple: parameter verification is set for each attribute in the solution entity, but only some attribute tasks need to be verified in a method, which can also be called classification,

It is identified in the form of interface, which can be compared with the serializable interface in Java collection

For example, there are user name, password, mobile phone number, email, age and gender. I want to test any two attributes (assuming mobile phone number and age),

Of course, you can directly add the corresponding annotation. If you change your mobile phone number and password, you must write two annotations,

Grouping is a good choice when there are too many fields and usage scenarios are frequent

Suppose there are two groups, interface group1 and interface group2

@Notnull (message = "name cannot be empty", groups = {group1. Class})

private String name;

After specifying the grouping, use @ validated (group1. Class) student student to verify only the specified grouping

(2) Custom verification

This is not recommended. The existing inspection types are basically enough. If there are more requirements, it is recommended to use logical judgment, which is clearer, convenient to record logs, and timely troubleshooting in case of problems.

6. References:

7. More discussion:

Q1: when submitting a form, the content of the form is not filled in. It is normal to submit, and the default is the empty string "". How to solve this problem

A1: verify whether it is empty at the front end, and use isempty method or @ notempty annotation at the back end

Q2: why hv000030: no validator could be found for type: Java lang.Integer.

A2: verification that does not belong to integer type may be added to integer attribute, such as @ notempty, @ length, etc

Q3: numerical parameter: when the input is null, the string "" cannot be parsed. After that, no other judgment can be made, and NPE will appear

A3: consider using custom exceptions. Use spring AOP to intercept exceptions and give custom error messages

8. Acknowledgement:

9. Conclusion:

That's all for today's sharing. You are welcome to like, forward, leave messages and make bricks~

Ppt link video link

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>