★ Spring Boot input verification
springboot支持两种校验方式:
1. Spring原生提供的 Validation,这种验证方式需要开发者手写验证代码,比较繁琐。就是普通的if判断
2. 使用JSR 303的校验,这种验证方式只需使用注解、即可以声明式的方式进行验证,非常方便。
★ JSR 303 verification steps:
(1) Add dependencies, then add JSR 303 annotations for the fields to be verified, and specify verification rules through these annotations.
(2) Add the @Validated annotation modification to the parameter being verified, and [follow] a parameter of type Errors to collect error prompts for verification failure.
In the processing method, the Errors parameter must be used to determine whether the verification fails. If the verification fails, the form page will be called back.
(3) Use the th:errors attribute on the page to output an error message for verification failure.
My summary of JSR 303 verification:
1. Add dependencies
2. On the domain entity class, add the annotations provided by dependencies on the fields to be verified, such as @Length verification length.
3. On the parameters of the controller method, add the @Validated annotation and an Errors type parameter for the parameters to be verified.
4. The front end uses th:errors to receive the error message that the output verification fails.
Code demonstration based on JSR 303 inspection:
Requirement: Verify the length of the name attribute value sent from the front end.
1. First add dependencies that support JSR 303 verification
2. Which parameter needs to be verified, add the corresponding annotation to the parameter.
Add the @Length annotation to the name attribute for length verification.
Add the @Validated annotation modification to the parameter being verified, and [follow] a parameter of type Errors to collect error prompts for verification failure.
In the processing method, the Errors parameter must be used to judge whether the verification fails, and the form page will be called back if the verification fails.
//@Validated Book book indicates that the book parameter needs to be parameter verified
//When the verification fails, all error information should be placed in the errors parameter
//If the verification fails, return to the form page. Otherwise, the code will still go down.
Use the th:errors attribute on the page to output error messages for verification failures.
Because the name attribute is being verified, th:errors here needs to get the name attribute value, which means that an error message for verifying the name field is output.
Test Results:
If the test fails, return to the index page.
Points to note: This prompt [the length needs to be between 6-20] is provided by the @Length annotation and is the default internationalization information.
The internationalization information means that if the page is in Chinese, it will prompt in Chinese, and
if the page is in English, it will prompt in English.
If you want to customize the prompt message, you can add the message attribute.
Demonstrate Validation verification
Validation provided natively by Spring requires developers to hand-write verification code, which is relatively cumbersome.
That is to write code input verification in the program. It’s just a normal if check
The front end also uses th:errors to receive error information.
Test result
successful: txt cannot be uploaded, only images can be uploaded
Then I got a 10M picture for testing, and the program crashed directly.