In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-31 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article focuses on "how to solve the @ Valid check invalid, BindingResult did not get the error", interested friends may wish to take a look. The method introduced in this paper is simple, fast and practical. Now let the editor take you to learn "how to solve the @ Valid check invalid, BindingResult did not get the error" bar!
Catalogue
@ Valid check invalidation
Problem description
Solution process
Use bindingResult for parameter verification
In the control class
Entity class OrderForm
@ Valid check failure problem description
Use @ Valid to verify the attribute stuTele in the entity class
Import javax.validation.constraints.Size;...@Size (min = 11 ~ max = 11 ~ ~ message = "Please enter 11-digit mobile phone number") private String stuTele
In Controller, check the property value of the student object passed in with the @ Valid annotation
Public String updateStuMsg (@ Valid Student student, BindingResult bindingResult, Model model, HttpSession httpSession) {.}
After passing in the error parameter, it is found that the error recorded in bindingResult is 0, which means the check is invalid.
Solution process
Check for introduced dependencies:
Org.hibernate hibernate-validator 7.0.1.Final javax.validation validation-api 2.0.1.Final
After switching the version of hibernate-validator to 5.4.1.Final, it is found that the verification is normal.
So it is positioned as a dependent version problem.
My springboot version is 2.5.1, and the check takes effect after replacing the dependency with the following
Org.springframework.boot spring-boot-starter-validation
Go into the spring-boot-starter-validation and look at its dependencies, including one of these
Org.hibernate.validator hibernate-validator 6.2.0.Final compile
So you can also use this version directly when you modify dependencies externally.
The dependency of validation-api does not need to be retained.
So how do you use the latest version of 7.0.1.Final? That's what the official document says.
Jakarta Bean Validation defines integration points with CDI (context and dependency injection for Jakarta EE). If your application runs in an environment that does not provide such out-of-the-box integration, you can use the Hibernate Validator CDI portable extension by adding the following Maven dependencies to your POM:
Example 1.3:Hibernate Validator CDI Portable extension Maven dependency
Org.hibernate.validator hibernate-validator-cdi 7.0.1.Final
Note that applications running on Java EE application servers usually do not need to add this dependency.
Then, you just need to replace all the previous dependencies with this one, and that's fine.
Org.hibernate.validator hibernate-validator-cdi 7.0.1.Final uses bindingResult for parameter verification in the control class @ RequestMapping ("/ create") public void create (@ Valid OrderForm orderForm, BindingResult bindingResult) {if (bindingResult.hasErrors ()) {log.error ("[incorrect order creation parameters], orderForm= {}", orderForm) / / bindingResult.getFieldError (). GetDefaultMessage () can get the error prompt throw new OrderException (ResultEnums.PARAM_ERROR.getCode (), bindingResult.getFieldError (). GetDefaultMessage ());} entity class OrderForm@Datapublic class OrderForm {@ NotEmpty (message = "required name") private String name;// buyer name @ NotEmpty (message = "Mobile number required") private String phone / / buyer's mobile phone number @ NotEmpty (message = "required address") private String address;// buyer address @ NotEmpty (message = "openid required") private String openid;// buyer Wechat @ NotEmpty (message = "Shopping cart cannot be empty") private String items / / Shopping cart} at this point, I believe you have a better understanding of "how to solve the @ Valid check invalid, BindingResult did not get the error", you might as well do it in practice! Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!
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.