Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to verify background parameters

2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/02 Report--

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!

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.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report