JSR-303 specification, Bean Validation

Source: Internet
Author: User

One:

JSR 303 is a sub-specification in Java EE 6, called the Bean Validation, and the official reference implementation is Hibernate Validator, which has nothing to do with hibernate ORM. JSR 303 is used to validate the value of a field in a Java bean.

This tutorial is translated from the Hibernate Validator 4.0 GA guide, and reference to the JSR 303 specification, with the exception of the JSR specification as far as possible, the document is written as far as possible without mentioning the underlying implementation of Hibernate Validator, but focus on the bean The validation specification itself. If there is something wrong, please give more advice, improve each other, thank you! I wrote the Chinese manual in the compressed package, and Hibernate Validator 4.0 GA put together, save everyone to download the jar file again Oh! Hey!

That's a lot of advice for you! Thank you! Ha!

Article source:http://www.iteye.com/topic/500928

Hibernate-validator-4.0.0.ga-dist.zip (4.8 MB): http://dl.iteye.com/topics/download/ d3374bdc-5a36-3270-8ffc-624356cf0716

Two:

JSR-303 is a sub-specification in Java EE 6, called the Bean Validation, and the official reference implementation is Hibernate Validator.
This implementation has no relationship to hibernate ORM. JSR 303 is used to validate the value of a field in a Java Bean.

JSR-303 is also strongly supported in Spring MVC 3.x, which makes it easy to verify the data submitted by the form in the controller.

JSR 303 built-in constraint rules:

@AssertTrue/@AssertFalse

    • Verify applicable field: Boolean
    • Note Description: Verify that the value is True/false
    • Property Description:-


@DecimalMax/@DecimalMin

    • Verify the applicable field: Bigdecimal,biginteger,string,byte,short,int,long
    • Note: Verify that the value is less than or equal to the specified decimal value, and be aware that there is a precision problem with decimals
    • Property Description: Public


@Digits

    • Verify the applicable field: Bigdecimal,biginteger,string,byte,short,int,long
    • Note: Verify that the numeric composition of the value is legal
    • Property Description: Integer: Specifies the number of digits in the integer part. Fraction: Specifies the number of digits in the fractional part.


@Future/@Past

    • Verify the applicable field: Date,calendar
    • Note Description: Verify that the value is after/before the current time
    • Property Description: Public


@Max/@Min

    • Verify the applicable field: Bigdecimal,biginteger,string,byte,short,int,long
    • Note Description: Verify that the value is less than or equal to the specified integer value
    • Property Description: Public


@NotNull/@Null

    • Verify applicable fields: Reference data type
    • Note Description: Verify that the value is non-empty/empty
    • Property Description: Public


@Pattern

    • Verify applicable field: String
    • Note Description: Verify that the value is equipped with a regular expression
    • Property Description: RegExp: Regular Expression Flags: Specifies an array of pattern.flag that represents the relevant options for the regular expression.


@Size

    • Validation applicable field: String,collection,map, array
    • Note Description: Verify that the value meets the length requirement
    • Property Description: Max: Specifies the maximum length, min: Specifies the minimum length.


@Valid

    • Validate applicable field: reference type
    • Note Description: Verify that the value requires recursive validation
    • Property Description: None



--------------------------------------------------------------------------------------------------------------- -------------------------------------------------------------------


When using spring MVC and JSR-303 annotations for server-side validation of form submissions,
The command object and the Bindingresult parameter of the @Valid callout must be next to each other. Otherwise
Data binding errors are thrown directly into an exception and are not encapsulated into a Bindingresult object.

Java code@RequestMapping (value="/login", Method=requestmethod.post)
    1. Public String Login (@Valid user User, Bindingresult br, map<string, object> model) {
    2. if (br.haserrors ()) {
    3. return "login";
    4. }
    5. return "redirect:/salary/list.do";
    6. }

Under

Three: Spring 3 MVC bindingresult result check attention point mvcspring

The use of Bindingresult result must be increased in the controller layer @modelattribute ("Fundspretransfer") it and Modelattribute are used at the same time, and the parameters are next to each other. That's not going to be reported. Errors/bindingresult argument declared without preceding model attribute exception.

Attention:

@RequestMapping ("/funds/transfer/save")
Public String Save (
@ModelAttribute ("Fundspretransfer") Fundspretransfer Fundspretransfer,
Bindingresult result, model model, HttpServletRequest request) {

Server-side Check
Fundstransfervalidator.validate (Fundspretransfer, result);
if (Result.haserrors ()) {
If there is an error, jump to the display page
return "Funds/transfer/show";
}

Double Transferamount = Fundspretransfer.gettransferamount ();
String comments = fundspretransfer.getcomments ();

Fundspretransfer.settransferip (Request.getremoteaddr ());
Query Fundsserialnumber
String Fundsserialnumber = Randomstringutils.random (9, False, True);
Fundspretransfer.setfundsserialnumber (Fundsserialnumber);//Set serial number
Save the transfer record.
Fundspretransferservice.persist (Fundspretransfer, GetUserName (),
Transferamount);

Pass the value to the next page.
String Fundsaccount = Request.getparameter ("Fundsaccount");//Fund Account
Model.addattribute ("Fundsaccount", Fundsaccount);
Model.addattribute ("Counterpartyfundsaccount",
Request.getparameter ("Counterpartyfundsaccount"));
Username isolated
Fundsbase fundsbase = Fundsbaseservice.findbyfundsaccount (Fundsaccount);
Baseaccount Baseaccount = Fundsbase.getbaseaccount ();
String username = baseaccount.getusername ();
Model.addattribute ("username", username);
Model.addattribute ("Transferamount", Transferamount);
Model.addattribute ("comments", comments);
Model.addattribute ("Fundsserialnumber", Fundsserialnumber);

return "Funds/transfer/show_next";
}

JSR-303 specification, Bean Validation

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.