STRUTS2 Custom Validation rules

Source: Internet
Author: User

Custom validation rules: (Learn)

In the Struts2 custom check rule:

1. Implement a validator interface.

2. Inherit Validatorsupport or Fieldvalidatorsupport in general development

* Validatorsupport: Not for a field check. Check confirmation password (compared to password field)

* Fieldvalidatorsupport: Check for a field.

Steps:

1. Writing a class inheritance Fieldvalidatorsupport

public void Validate (Object obj) throws Validationexception {

Get field Name

String name = GetFieldName ();

Get the value of a field

Object value = GetFieldValue (name, obj);

Judging this value

if (value instanceof Integer) {

Integer i = (integer) value;

if (I < 0) {

Addfielderror (name, obj);

}

}

}

2. Register the Validator: Create a validators.xml under SRC

* The introduction of the Dtd:xwork core package has a XWORK-VALIDATOR-CONFIG-1.0.DTD

* Configure in this XML:

<validators>

<validator name= "AAA" class= "Cn.itcast.struts2.demo5.MyAgeValidator" ></validator>

</validators>

STRUTS2 Custom Validation rules

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.