Struts2 input verification (4)-custom validators and programmatic Verification

Source: Internet
Author: User
1. Custom validators 1. Implementation steps:

1) define a validator class

The user-defined validators must implement the validator interface. Because validatorsupport and fieldvalidatorsupport implement the validator interface, they can inherit the validatorsupport or fieldvalidatorsupport

I. If you need a common verification program, you can inherit the validatorsupport class;

II. If you need a field verification program, you can inherit the fieldvalidatorsupport class;

Iii. If the validators need to accept an input parameter, they need to add a corresponding attribute for this parameter.

2) Configure (Register) validators in the configuration file

Register the validators: the User-Defined validators must be registered in a validators. xml file in the class path.

By default, struts2 loads the validators. xml file in the root directory of the class path and loads the validators in the file. This file is defined in the same way as the configuration file of struts2's built-in default validators (that is, the default. xml file is located under com. opensymphony. xwork2.validator. validators), for example:

If no validators are specified in the class path. opensymphony. xwork2.validator. default under validators. XML validators load (that is, struts2 built-in validators) 3) User-Defined validators use the same method as struts2 built-in validators. 2. Example: Custom validators

 Requirements:Customize an 18-digit ID card validators

1) Compile the validators class idcardvalidator:

1 package com. atguigu. struts2.validation. app; 2 Import com. opensymphony. xwork2.validator. validationexception; 3 Import com. opensymphony. xwork2.validator. validators. fieldvalidatorsupport; 4IdcardvalidatorExtendsFieldvalidatorsupport{5 @ override 6 Public voidValidate(Object object) throws validationexception {7 // 1. obtain the field name and value 8 string fieldname = getfieldname (); 9 object value = This. getfieldvalue (fieldname, object); 10 // 2. verification 11IdcardIdcard = new idcard (); 12 boolean result = idcard. Verify (string) value); 13 // 3. If verification fails, then... 14 if (! Result) {15 addfielderror (fieldname, object); 16} 17} 18} 19 20 // === The following is the idcard class === 21 package COM. atguigu. struts2.validation. APP; 22 public classIdcard{23 final int [] Wi = {7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2, 1}; 24 final int [] Vi = {1, 0, 'x', 9, 8, 7, 6, 5, 4, 3, 2 }; 25 private int [] Ai = new int [18]; 26 Public idcard () {}27 public Boolean verify (string idcard) {28 If (idcard. length () = 15) {29 idcard = uptoeighteen (idcard); 30} 31 if (idcard. length ()! = 18) {32 return false; 33} 34 string verify = idcard. substring (17, 18); 35 if (verify. equals (getverify (idcard) {36 return true; 37} 38 return false; 39} 40 Public String getverify (string eightcardid) {41 int remaining = 0; 42 if (eightcardid. length () = 18) {43 eightcardid = eightcardid. substring (0, 17); 44} 45 if (eightcardid. length () = 17) {46 int sum = 0; 47 for (INT I = 0; I <17; I ++) {48 string K = eightcardid. substring (I, I + 1); 49 Ai [I] = integer. parseint (k); 50} 51 for (INT I = 0; I <17; I ++) {52 sum = sum + wi [I] * Ai [I]; 53} 54 remaining = sum % 11; 55} 56 return remaining = 2? "X": string. valueof (VI [remaining]); 57} 58 Public String uptoeighteen (string fifteencardid) {59 string eightcardid = fifteencardid. substring (0, 6); 60 eightcardid = eightcardid + "19"; 61 eightcardid = eightcardid + fifteencardid. substring (6, 15); 62 eightcardid = eightcardid + getverify (eightcardid); 63 return eightcardid; 64} 65}

2) create a directory in the src directoryValidators. xml fileAnd register the User-Defined validators.

<validators>    <validator name="idcard" class="com.atguigu.struts2.validation.app.IDCardValidator"/></validators>

3) use it in the authentication configuration file

<validators>    <field name="idCard">        <field-validator type="idcard">            <message>It is not a idCard!</message>        </field-validator>    </field></validators>
Ii. Programming Verification

Struts2 providesValidateable InterfaceTo enable the action class to implement this interface to provide a programmatic verification function.

The actionsupport class has implemented the validateable interface. Therefore, the validateable interface can be indirectly implemented by inheriting the actionsupport class.

For example, determine whether the name is null:

 

 

 

Note: The struts2 tutorial is from Shang Silicon Valley-Ji Gang-struts2. Thank you for sharing your thoughts with Shang Silicon Valley and Yu Gang.

Struts2 input verification (4)-custom validators and programmatic Verification

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.