[Share] validator of deep struts

Source: Internet
Author: User
Tags valid email address

In struts, validator is very useful. It can help users get away from tedious page verification work and put the page verification work in the configuration file, struts has the following built-in validators that we often use:

· The validatebyte check value can be safely converted to byte
· Validatecreditcard is a valid credit card number.
· The validatedate check value is a valid date.
· Validatedouble check value can be safely converted to double
· The validateemail check value is a valid email address.
· Validatefloat check value can be safely converted to double
· Validateinteger check value can be safely converted to int
· The validatelong check value can be safely converted to long
· Validatemask checks that the values comply with the mask rules. The mask adopts the regular expression.
· The length of the validateminlength check value is greater than or equal to the specified length.
· The length of the validatemaxlength check value is smaller than the specified length.
· The valid range of the validaterange check value is within the specified range
· Validaterequired check value is not null or length> 0
· The validateshort check value can be safely converted to short

These validators cover most of the items that need to be verified. Today we will roughly review how validator is applied in struts. If there is time, I will show you how it is implemented in actual projects.

Step 1: Enable the validator plug-in

Validator plug-in has been built in struts, but by default is not enabled, we first want to modify the struts-config.xml file, in the fileLast(Remember, it's the last one ~!), Add the following code to enable it:

<Plug-in classname = "org. Apache. Struts. validator. validatorplugin">
<Set-Property = "pathnames"
Value = "/org/Apache/struts/validator/validator-rules.xml,/WEB-INF/validation. xml"/>
</Plug-in>

Step 2: Generate actionform Bean

Java. Lang. Object
Org. Apache. Struts. Action. actionform
Org. Apache. Struts. validator. validatorform
Org. Apache. Struts. validator. validatexceptionform
Java. Lang. Object
Org. Apache. Struts. Action. actionform
Org. Apache. Struts. Action. dynaactionform
Org. Apache. Struts. validator. dynavalidatorform
Org. Apache. Struts. validator. dynavalidatexceptionform

From the above inheritance relationship, we can clearly see that validatexceptionform/dynavalidatexceptionform inherits from actionform. When we implement our own actionform Bean, we can inherit validatexceptionform/dynavalidatexceptionform. The content is the same as our usual actionform bean.

Step 3: Configure validator

Now that the preparation is complete, we can create a new validation. xml file in the WEB-INF Based on the configuration in step 1,

<FormSet>
<Form name = "/customer/cust001">
<Field property = "usersel" depends = "required">
<Arg0 key = "option" resource = "false"/>
</Field>

<Field property = "recptnuminput" depends = "needvalidate, intrange">
<Arg0 key = "pay-as-you-go" resource = "false"/>
<Arg key = "prompt. min" position = "1"/>
</Field>

<Field property = "Max" depends = "intrange, maxlength">
<Arg key = "prompt. Max" position = "0"/>
<Arg name = "maxlength" Key = "$ {var: maxlength}" resource = "false" position = "1"/>
<Var>
<Var-Name> maxlength </var-Name>
<Var-value> 10 </var-value>
</Var>
</Field>
</Form>
</FormSet>

This section describes the meaning of each element.
<Form> Needless to say, it defines the form to be verified.
<Field> is used to determine the field to be verified. The depends attribute indicates the validators that the field depends on.
<Arg> it is used to replace a part of information or provide required parameter values for the verification method.
You can input parameters in either of the following ways: First, <arg0>, <arg1>... (up to four), and second. Use the position attribute in <Arg> to set the input position.
The key attribute specifies the keyword of a resource file to replace the part to be replaced in MSG. If you want to set a clear text instead of the keyword of the resource file, you can set the resource attribute to false. In this case, you can set the key attribute to a clear text, for example, "ID card ".
The name Property specifies the name of the validation rule that MSG will use, and the property value must be a validation rule defined in the validation-rules.xml file.
Note:For a field, more than one verification rule is required, for example, depends = "required, date". When an error occurs for each of the multiple verification rules, some parameter information is input, if they require the same number of parameters, names, and order, you only need to define <arg0.0..., <arg1... and so on. But what if they need different numbers, names, and sequences of parameters? If we define only one arg0 for the first input parameter, which authentication message is used for this arg0, therefore, the name attribute of the ARG label must be used to verify the service of this parameter.

The last child element of the field element is the VaR element. The field element can contain 0 to multiple var elements. The VaR element can set the parameters of the validation rules used by the field, vaR-name parameter name, Var-value specifies the parameter value.

<MSG> similar to <Arg>, the three attributes are as follows:

<Field property = "datestart" depends = "date, daterange">
<MSG name = "date, daterange" Key = "80000" resource = "true"/>
<Arg0 key = "Start Date" resource = "false"/>
<Var>
<Var-Name> date_mode </var-Name>
<Var-value> yyyyy </var-value>
</Var>
</Field>

The name attribute is the same as the name attribute of <Arg>.
The key attribute specifies the keyword of a resource file. When verification fails, the information represented by this keyword will be added to actionerror.
The resource attribute is the same as that of <Arg>.

In this way, all concepts are clearly explained. Next let's take a look at how the actual project is implemented.

First, you can configure multiple validator files for different business logic.

<! -- ====================== Business name: validatorplugin Logging
<Plug-in classname = "org. Apache. Struts. validator. validatorplugin">
<Set-Property = "pathnames"
Value = "/WEB-INF/Struts-config/validator-rules.xml,
/WEB-INF/Struts-config/validation-login.xml,
/WEB-INF/Struts-config/bankuser/validation-tekou.xml,
/WEB-INF/Struts-config/bankuser/validation-kirin.xml,
/WEB-INF/Struts-config/enduser/validation-qbs.xml "/>
</Plug-in>
<! -- ====================== Business name: validatorplugin already exist ===================================================-->

Second, you can customize rules in each validation file.
<Validator name = "fullchar"
Classname = "com. ws. kokey. util. validator. unitvalidator"
Method = "validatecharacterfull"
Methodparams = "Java. Lang. object,
Org. Apache. commons. validator. validatemedition,
Org. Apache. commons. validator. Field,
Org. Apache. Struts. Action. actionerrors,
Javax. servlet. http. httpservletrequest"
MSG = "のチェッ">
</Validator>

The specific implementation can be written in the defined class.
Well, I can't write it again today. It has been written to PM since the morning. Write again next time.

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.