Get the App
SLTechnology News&Howtos  ›  Development  › 

How to use the @ Validated annotation in springmvc

Shulou Source: shulou.com Published: 2022-06-03 11:00:22 11月15日 Update

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!

Tags: Configuration information files comments content properties articles errors maximum less most age method more users user names knowledge structure industry information Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Shulou Technology vpn macOS MariaDB NVidia