[Original]java Web Learning Note 71:struts2 Learning path--struts2 common built-in verification program and note Point, short-circuit verification, non-field validation, error message reuse

Source: Internet
Author: User

The purpose of this blog: ① summary of their own learning process, equivalent to study notes ② to share their own experience to everyone, learn from each other, communication, not commercialcontent inevitably appear problems, welcome to correct, exchange, discussion, you can leave a message, can also be contacted by the following ways. I Internet technology enthusiasts, Internet technology enthusiastsWeibo: Ivan is in 0221qq:951226918

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

1.struts2 built-in validator: reference documentation. /struts-2.3.15.3-all/struts-2.3.15.3/docs/ww/docs/conversion-validator.html

Field validation with:<s:fielderror fieldname= "" ></s:fielderror> display error message

1 <validators>2     <Validatorname= "Required"class= "Com.opensymphony.xwork2.validator.validators.RequiredFieldValidator"/>3     <Validatorname= "Requiredstring"class= "Com.opensymphony.xwork2.validator.validators.RequiredStringValidator"/>4     <Validatorname= "int"class= "Com.opensymphony.xwork2.validator.validators.IntRangeFieldValidator"/>5     <Validatorname= "Long"class= "Com.opensymphony.xwork2.validator.validators.LongRangeFieldValidator"/>6     <Validatorname= "Short"class= "Com.opensymphony.xwork2.validator.validators.ShortRangeFieldValidator"/>7     <Validatorname= "Double"class= "Com.opensymphony.xwork2.validator.validators.DoubleRangeFieldValidator"/>8     <Validatorname= "Date"class= "Com.opensymphony.xwork2.validator.validators.DateRangeFieldValidator"/>9     <Validatorname= "expression"class= "Com.opensymphony.xwork2.validator.validators.ExpressionValidator"/>Ten     <Validatorname= "Fieldexpression"class= "Com.opensymphony.xwork2.validator.validators.FieldExpressionValidator"/> One     <Validatorname= "Email"class= "Com.opensymphony.xwork2.validator.validators.EmailValidator"/> A     <Validatorname= "url"class= "Com.opensymphony.xwork2.validator.validators.URLValidator"/> -     <Validatorname= "Visitor"class= "Com.opensymphony.xwork2.validator.validators.VisitorFieldValidator"/> -     <Validatorname= "Conversion"class= "Com.opensymphony.xwork2.validator.validators.ConversionErrorFieldValidator"/> the     <Validatorname= "Stringlength"class= "Com.opensymphony.xwork2.validator.validators.StringLengthFieldValidator"/> -     <Validatorname= "Regex"class= "Com.opensymphony.xwork2.validator.validators.RegexFieldValidator"/> -     <Validatorname= "Conditionalvisitor"class= "Com.opensymphony.xwork2.validator.validators.ConditionalVisitorFieldValidator"/> - </validators>

1) Required: Ensure that the value of a given field is not NULL ""

2) Requiredstring: Ensure that the value of a given field is neither null nor blank.

Trim parameters. The default is true, which means that struts rejects the front and back spaces before validating the field value.

3) Stringlength: Verify that a non-empty field value is not sufficient in length.

MinLength: The minimum length of the related field. If this parameter is not given, the field will not have a minimum length limit

MaxLength: The maximum length of the related field. If this parameter is not given, the field will not have a maximum length limit

Trim: Before and after verifying whether to remove space before and after verification

4) Date: Ensure that the value of a given date field falls within a given range

Max: The maximum value of the related field. If this parameter is not given, the field will not have a maximum limit

Min: The minimum value of the related field. If this parameter is not given, the field will have no minimum limit

5) Email: Check if the given String value is a legitimate email

6) URL: Check if the given String value is a valid URL

7) Regex: Checks whether the value of a given field matches a given regular expression pattern.

     expresssion*: A regular expression to match

CaseSensitive: Whether to distinguish the case of letters. Default is True

Trim: Whether to remove the front and rear spaces. Default is True

8) Int: Checks whether a given integer field value is within a range

Min: The minimum value of the related field. If this parameter is not given, the field will have no minimum limit

Max: The maximum value of the related field. If this parameter is not given, the field will not have a maximum limit

9) Conversion: Checks whether the type conversion to the given Action property causes a conversion error. The validator can also add a custom message based on the default type conversion message

Expression and fieldexpression: used to verify whether a given field satisfies a OGNL expression.

The former is a non-field validator, which is a field validator.

The former generates an action error when validation fails, and the latter generates a field error when validation fails

expression*: The OGNL expression used for validation

2. Short Circuit verification

  1) <validator .../> element and <field-validator .../> element You can specify an optional short-circuit property that Specifies whether the validator is a short validator, and the default value is False.

2) for multiple validators within the same field, if a short-circuit validator fails validation, the other validator does not continue to validate.

1 <!DOCTYPE validators Public2 "-//apache struts//xwork Validator 1.0.2//en"3 "Http://struts.apache.org/dtds/xwork-validator-1.0.2.dtd">4 5 <validators>6     7     <!--validated against the age attribute. Field-based validation -8      <Fieldname= "Age">9      Ten      <!--set the short-circuit verification, if the current validation does not pass, then the following verification is no longer - One          <Field-validatortype= "Conversion"short-circuit= "true"> A              <message>Conversion Error occurred</message> -          </Field-validator> -          <Field-validatortype= "int"> the              <paramname= "min">20</param> -              <paramname= "Max">50</param> -              <message>Age needs to be between ${min} and ${max}</message> -          </Field-validator> +      </Field> - </validators>

  

3. non-field validation: No validation for a field

1 <Validatortype= "expression">2          <paramname= "expression"><! [Cdata[Password==password2]]></param>3          <message>Password isn't equals to Password2</message>4 </Validator>

Display error messages for non-field validation, using the s:actionerror tag:

4. Reuse of error messages: Different fields use the same validation rules, and the same response message is used.

In the internationalization file

Error.int=${gettest (FieldName)} needs to be ${min} and ${max}

Age=\u5e74\u9f84

Count=\u6570\u91cf

5. Custom Validator

1) Custom validator must implement Validator interface

2) Validatorsupport and Fieldvalidatorsupport implement the Validator interface

   - If you need an ordinary verification program , you can inherit the Validatorsupport class

- If you need a field validator , you can inherit the Fieldvalidatorsupport class

- If the validator needs to accept an input parameter , you need to add a corresponding property to this parameter

3) Registration Validator: The custom validator needs to be registered in a Validators.xml file in the classpath:

The validation framework first finds the Validators.xml file in the root directory, does not find the Validators.xml file, and the validation framework invokes the default authentication settings, which is the configuration information in the Default.xml.

  

 

 

[Original]java Web Learning Note 71:struts2 Learning path--struts2 common built-in verification program and note Point, short-circuit verification, non-field validation, error message reuse

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.