In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
Editor to share with you how to use the @ Validated annotation in springmvc, I believe most people do not know much about it, so share this article for your reference, I hope you will learn a lot after reading this article, let's learn about it!
1. The error message of the check can be configured directly on the bean property or in properties.
two。 Configure the validator bean to the annotation driver
3. Add @ Validated before the pojo that needs to be verified, and BindingResult bindingResult to receive the error message after the pojo that needs to be verified
Note: @ Validated and BindingResult bindingResult appear in pairs, and the order of parameters is fixed (before and after).
Configuration required to use validated:
1. Jar package
2. Application.xml
Classpath:testErrMessages
3. TestErrMessage.properties file
File structure:
Contents of the file:
4. Bean configuration, omitting get/set method
Public class ValidBean {/ / Direct configuration error message @ NotBlank (message= "username cannot be empty", groups= {UserGroup1.class}) private String name;@Max (value=120, message= "up to 120") private Integer age;// uses the error message in the configuration file, key: message=" {key} "in properties. {}: placeholder @ NotBlank (message= "{validator.user.address}") private String address;}
5. Controller
@ Controller@RequestMapping ("/ test/") public class ValidTestController {/ * * @ desc: verify the attribute without grouping * @ auth: zona * 2:35:42 on February 19, 2017 * @ param bean * @ param bindingResult * @ return * / @ RequestMapping ("valid") @ ResponseBodypublic Result testValid (@ Validated ValidBean bean, BindingResult bindingResult) {Result result = new Result (); Map retMap = new HashMap (); StringBuilder sb = new StringBuilder () If (bindingResult.hasErrors ()) {List errors = bindingResult.getAllErrors (); for (ObjectError err: errors) {sb.append (err.getDefaultMessage () + ";");} result.setErrInfo (sb.toString ()); result.setErrNo (1); return result;} retMap.put ("bean", bean); result.success (); result.setData (retMap); return result } / * * @ desc: check only the attributes that belong to the userGroup group * @ auth: zona * 2:37:03 on February 19, 2017 * @ param bean * @ param bindingResult * @ return * / @ RequestMapping ("group/valid") @ ResponseBodypublic Result testGroupValid (@ Validated (value=UserGroup1.class) ValidBean bean, BindingResult bindingResult) {Result result = new Result (); Map retMap = new HashMap (); StringBuilder sb = new StringBuilder () If (bindingResult.hasErrors ()) {List errors = bindingResult.getAllErrors (); for (ObjectError err: errors) {sb.append (err.getDefaultMessage () + ";");} result.setErrInfo (sb.toString ()); result.setErrNo (1); return result;} retMap.put ("bean", bean); result.success (); result.setData (retMap); return result;}}
The above is all the content of the article "how to use @ Validated annotations in springmvc". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!
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.