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 use Spring to inject Hibernate verification framework

2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

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

This article introduces the knowledge of "how to use Spring to inject Hibernate verification framework". In the operation of actual cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!

Spring injection Hibernate verification framework Spring configuration file Hibernate built-in validation constraints are annotated as shown in the following table

(from hibernate validator reference):

Validate comments

Validated data type

Description

@ AssertFalse

Boolean,boolean

Verify that the element value of the annotation is false

@ AssertTrue

Boolean,boolean

Verify that the element value of the annotation is true

@ NotNull

Any type

Verify that the element value of the annotation is not null

@ Null

Any type

Verify that the element value of the annotation is null

@ Min (value= value)

BigDecimal,BigInteger, byte

Short, int, long, etc. Any Number or CharSequence (which stores numbers) subtypes

Verify that the element value of the annotation is greater than or equal to the value specified by @ Min

@ Max (value= value)

Same as @ Min requirement

Verify that the element value of the annotation is less than or equal to the value specified by @ Max

@ DecimalMin (value= value)

Same as @ Min requirement

Verify that the element value of the annotation is greater than or equal to the value specified by @ DecimalMin

@ DecimalMax (value= value)

Same as @ Min requirement

Verify that the element value of the annotation is less than or equal to the value specified by @ DecimalMax

@ Digits (integer= integer places, fraction= decimal places)

Same as @ Min requirement

Verify the upper limit of the integer and decimal places of the element value of the annotation

@ Size (min= lower limit, max= upper limit)

String, Collection, Map, array, etc.

Verify that the element value of the annotation is within the specified range of min and max (inclusive), such as character length and set size.

@ Past

Java.util.Date

Java.util.Calendar

Date type of the Joda Time class library

Verify that the element value (date type) of the annotation is earlier than the current time

@ Future

Same as @ Past requirement

Verify that the element value (date type) of the annotation is later than the current time

@ NotBlank

CharSequence subtype

Verify that the element value of the annotation is not empty (not null, and the length is 0 after removing the first space), unlike @ NotEmpty,@NotBlank, which only applies to the string and removes the first space of the string when comparing

@ Length (min= lower limit, max= upper limit)

CharSequence subtype

Verify that the element value length of the annotation is within the interval of min and max

@ NotEmpty

CharSequence subtype, Collection, Map, array

Verify that the element value of the annotation is not null and is not empty (string length is not 0, collection size is not 0)

@ Range (minimum min=, maximum max=)

BigDecimal,BigInteger,CharSequence, byte, short, int, long and other atomic types and packaging types

Verify that the element value of the annotation is between the minimum and maximum values

@ Email (regexp= regular expression

Mode of flag= flag)

CharSequence subtype (such as String)

Verify that the element value of the annotation is Email, or you can specify a custom email format through regexp and flag

@ Pattern (regexp= regular expression

Mode of flag= flag)

String, a subtype of any CharSequence

Verify that the element value of the annotation matches the specified regular expression

@ Valid

Any non-atomic type

Specify the objects associated with recursive validation

If there is an address object attribute in the user object, if you want to verify the address object together when verifying the user object, add the @ Valid annotation to the address object to cascade the verification.

Springmvc uses the jar package required by Hibernate's validation framework validation1 and validator in Hibernate

Hibernate-validator-4.3.1.Final.jar

Jboss-logging-3.1.0.GA.jar

Validation-api-1.1.0.Final.jar

Configure the validator classpath:CustomValidationMessages 3. Register the verifier with the processor adapter 4. Add the verification rule package acm.user.po; import javax.validation.constraints.NotNull to the pojo Import javax.validation.constraints.Size; public class User {private Integer id; / / check name 1 to 30 characters between / / message is the error message @ Size (min=1, max=30, message= "{user.name.length.error}") private String name; / / non-empty check @ NotNull (message= "{user.num.noNull}") private String num; private String sex; private String tel;. . . . Public Integer getId () {return id;} public void setId (Integer id) {this.id = id;}}

Code in the configuration file

User.name.length.error= Please enter a name of 1 to 30 characters user.num.noNull= Please enter the date of production of the product, controller test @ RequestMapping (value = "updateUser") public String updateUser (@ Validated User user, BindingResult bindingResult) {List allErrors = bindingResult.getAllErrors (); / / get error information for (ObjectError e: allErrors) {/ / output error information System.out.println (e.getDefaultMessage ()) } try {int count = userService.updateUser (user);} catch (Exception e) {e.printStackTrace ();} return "message";} packet check: define multiple check packets, in which some rules are defined

Different parity groups are defined in each controller method

Define an interface that can be used to hold a grouping without writing.

Write down in pojo which packet each verified field belongs to / / check name 1 to 30 characters between / / message is prompted to verify the error message / / identify which packet this check belongs to, group can also belong to multiple packets @ Size (min=1, max=30, message= "{user.name.length.error}", groups= {Validation1.class}) private String name Use the parameter in packet check @ Validated in controller to point to the inspection packet @ RequestMapping (value= "updateUser") public String updateUser (@ Validated (value= {Validation1.class}) User user, BindingResult bindingResult) {if (bindingResult.hasErrors ()) {List allErrors = bindingResult.getAllErrors (); / / get the error message for (ObjectError e: allErrors) {/ / output error message System.out.println (e.getDefaultMessage ()) }} try {int count = userService.updateUser (user);} catch (Exception e) {e.printStackTrace ();} return "message";} "how to inject Hibernate verification framework using Spring" ends here. Thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!

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

Development

Wechat

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

12
Report