Get the App
SLTechnology News&Howtos  ›  Internet Technology  › 

How to verify background parameters

Shulou Source: shulou.com Published: 2022-06-02 02:59:44 10月17日 Update

This article mainly explains "how to verify background parameters". The content of the explanation is simple and clear, and it is easy to learn and understand. Please follow the editor's train of thought to study and learn "how to verify background parameters".

In the past, when dealing with parameter verification of web requests, it was necessary to have code similar to the following

If (age

< 18) { System.out.println("请输入正确的年龄(太小了)");}if(age >

{System.out.println ("Please enter the correct age (too old)");} if (name = = null | | name.trim (). Length () = 0) {System.out.println ("name cannot be empty");} if (name! = null & & (name.trim (). Length ())

< 2 || name.trim().length() >

20)) {System.out.println ("name length error");}

You can do this with the validation plug-in, and you need to introduce dependencies:

Org.hibernate hibernate-validator 6.0.14.Final org.glassfish javax.el 3.0.0

The full code is as follows:

Package test;import javax.validation.ConstraintViolation;import javax.validation.Validation;import javax.validation.constraints.*;import java.util.Arrays;import java.util.List;import java.util.Set;public class ValidateTest {public static void main (String [] args) {UserInfo user = new UserInfo (0, ", null," s "); System.out.println ("-old method-") OldMethod (user); System.out.println ("- new method -"); newMethod (user);} private static void oldMethod (UserInfo user) {String name = user.getName (); int age = user.getAge (); List tags = user.getTags (); if (age)

< 18) { System.out.println("请输入正确的年龄(太小了)"); } if(age >

{System.out.println ("Please enter the correct age (too old)");} if (name = = null | | name.trim (). Length () = 0) {System.out.println ("name cannot be empty");} if (name! = null & & (name.trim (). Length ())

< 2 || name.trim().length() >

20) {System.out.println ("name length error");} if (tags.size ()

< 1 || tags.size() >

10) {System.out.println ("enter at least one tag, and a maximum of 10 tags are supported");} for (String tag: tags) {if (tag = = null | | tag.trim (). Length () = 0) {System.out.println ("tag content cannot be empty") } private static void newMethod (UserInfo user) {Set errorSet = Validation.buildDefaultValidatorFactory () .getValidator () .validate (user); errorSet.forEach (item- > {System.out.println (item.getMessage ());}) } static class UserInfo {@ Min (value = 18, message = "Please enter the correct age (too young)") @ Max (value = 100,message = "Please enter the correct age (too old)") int age / / @ Pattern (regexp = "[Amurz] | [Amurz]") @ NotBlank (message = "name cannot be empty") @ Size (min = 2, max = 20, message = "name length error") String name; @ NotNull @ Size (min = 1, max = 10, message = "enter at least one tag, support a maximum of 10 tags") List

< @NotBlank(message = "标签内容不能为空") @Size(min = 1, max = 10, message = "标签内容长度限制1-10个字符") String>

Tags; public UserInfo (int age, String name, String... Tags) {this.age = age; this.name = name; this.tags = Arrays.asList (tags);} public int getAge () {return age;} public void setAge (int age) {this.age = age;} public String getName () {return name } public void setName (String name) {this.name = name;} public List getTags () {return tags;} public void setTags (List tags) {this.tags = tags Thank you for your reading. The above is the content of "how to verify background parameters". After the study of this article, I believe you have a deeper understanding of how to verify background parameters. Specific use also needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!

Tags: Name input parameters validation tags background age content error length learning maximum code support indispensable that is ideas situations plug-ins articles Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Xiaomi Redmi vpn NVidia Apple