In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-01 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article mainly introduces "how to understand the technical landing of form verification". In the daily operation, I believe that many people have doubts about how to understand the technical landing of form verification. The editor consulted all kinds of data and sorted out simple and useful operation methods. I hope it will be helpful for you to answer the doubt of "how to understand the technical landing of form verification"! Next, please follow the editor to study!
One: Overview
When submitting a form, the parameters are generally checked and returned to the user for prompt in time, so that the user can enter it correctly.
We use Validator + BindResult
Two: verification rules
Validator can easily make verification rules and automatically help you complete the verification.
(1) add dependencies
Pom.xml
Org.springframework.boot spring-boot-starter-validation
(2) Notes
Wrapper classes of basic types
@ Null: the limit must be null, and any non-basic data type can be verified
@ NotNull: the restriction must not be null. Wrapper classes of basic types can be used, but the strings "," and "" will be passed.
@ Max (value): the limit must be a number not greater than the specified value
@ Min (value): the limit must be a number not less than the specified value
@ DecimalMax (value): the limit must be a number not greater than the specified value
@ DecimalMin (value): the limit must be a number not less than the specified value
@ Digits (integer,fraction): the limit must be a decimal, and the number of digits of the integer cannot exceed integer, and the number of decimal places cannot exceed fraction
@ Range (min=,max=,message=): the annotated element must be within the appropriate scope
Date Typ
@ Future: the limit must be a future date
@ Past: the limit must be a past date
String
@ Pattern (value): the restriction must conform to the specified regular expression
@ Size (max,min): limit character length must be between min and max, or it can be used to check collections
@ NotBlank (message =): the verification string is not null and the length must be greater than 0. It is suitable for String parameter verification.
@ Email: the annotated element must be an email address
@ Length (min=,max=): the size of the annotated string must be within the specified range
@ NotEmpty: the commented string must be non-empty
Third: modify the entity class
First of all, add comments to the fields that need to be verified in the input parameter. Each comment corresponds to different verification rules, and the information after verification failure can be made:
@ Datapublic class EmployeeForm {private Integer id; @ NotEmpty (message = "employee name cannot be empty") private String name; / / name @ NotEmpty (message = "employee gender cannot be empty") private String sex; / / gender @ Past (message = "employee birthday must be earlier than that day") private Date birthday; / / birthday @ NotEmpty (message = "employee phone cannot be empty") private String telephone / / phone @ NotEmpty (message = "user name cannot be empty") private String username; / / user name @ NotNull (message = "employee's department cannot be empty") private Integer deptFk; / / department id private String remark; / / remarks}
Four: accept and verify
You only need to add @ Validated to the interface in controller that needs to be verified.
@ PostMappingpublic R save (@ RequestBody @ Validated EmployeeForm employeeForm) {employeeService.save (employeeForm); return R.ok ();}
Five: define exception handling
Write global exception handlers
@ Slf4j@RestControllerAdvicepublic class GlobalExceptionHandler {@ ExceptionHandler (BindException.class) public R error (BindException e) {e.printStackTrace (); return R.error () .message (e.getBindingResult (). GetAllErrors (). Get (0). GetDefaultMessage ());} at this point, the study of "how to understand the technical landing of form verification" is over, hoping to solve everyone's doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.