Use validator validation framework in Struts

Source: Internet
Author: User
Tags constant regular expression resource

Validatro framework to become part of Jakarta's public projects, you can download a separate validator framework from http://jakarta.apache.org/commons/, which has been stuck in struts.

Validator mainly relies on two jar packs

Jakarta-oro.jar:-provides a set of classes that handle text, with text substitution, filtering, and segmentation.

Commons-validator.jar: Provides a simple, extensible validation framework that includes common validation methods and validation rules.

Using this frame in struts, you need to add these two packages and it feels good to use them.

Here is a brief introduction.

Validator uses a two-XML file to configure validation rules, respectively, Validation.xml,validator-rules.xml. In struts applications, it needs to be placed in the Web-inf directory.

1. Validator-rules.xml

This file contains a set of validation rules that apply to all struts applications. Generally, you do not need to modify this file unless you want to modify or extend the default rule. If you want to put it in another XML file instead of adding it directly to the Validator-rules.xml file, you do not have to modify the Validator-rules.xml file when the validator is upgraded.

2. Validator.xml file

This file is for specific struts applications, and he can configure validation rules for Actionform in the application. Instead of coding to implement validation.

For example, a validation landing form example, requires that the user name must be filled in, secret requirements must be filled, maximum, minimum length and its content requirements.

Code

<form-validation>
<global>
<!--A string consisting of numbers, 26 English letters, or underscores;
<const Ant>
<constant-name>pwd</constant-name>
<onstant-value>^\w+$</onstant-value& Gt       
</constant>
</global>
<formset>
<form name= "LoginForm"
         <!--the user name here uses the mailbox, the authentication uses the regular expression to authenticate the property=, the
<field user.userid depends= "required"
<arg0 key= "User.userid"/>
</field>
<field property= "User.userpwd" depends= , Minlength,maxlength,mask "
<!--msg name=" Mask "key=" Errors.invalid "/-->
<arg0 key = "User.userpwd"/>
<arg1 name= "minlength" key= "${var:minlength}" resource= "false"/>
&L T;arg2 name= "maxlength" key= "${var:maxlength}" resource= "false"/>
<var>
<var-name >mask</var-name
<var-value>^\w+$</var-value>
</var>
<var>
<var-name>minlength</var-name>
<var-value>6</var-value>
</var& Gt
<var>
<var-name>maxlength</var-name>
<var-value>20< /var-value>
</var>
</field>
</form>
</formset>
< /form-validation>

The syntax for validator.xml and validator-rules.xml files is not explained here.

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.