Struts2 verification framework configuration and common validation rules of validation. xml

Source: Internet
Author: User
Validation. XML Naming rules and placement path:

File Name: <actionclassname>-validation. xml
<Actionclassname> is the name of the action class to be verified. To save this file to the same directory as the class file.

If the action class has multiple action instances in the struts configuration, the rules for verifying the file name of an action are as follows:

File Name: <actionclassname>-<aliasname>-validation. xml

Example: UserAction-login-validation.xml

(Note: The above <aliasname> is not the method name, but the name of the Action configured in struts. xml)

Content example of validation. xml:

<? XML version = "1.0" encoding = "UTF-8"?> <Br/> <! Doctype validators public "-// opensymphony group // xwork validator 1.0.2/EN" <br/> "http://www.opensymphony.com/xwork/xwork-validator-1.0.2.dtd"> <br/> <validators> <br/> <field name = "username"> <br/> <field-validator type = "requiredstring"> <br/> <Param name = "trim"> true </param> <br/> <message> enter the user name </message> <br/> </field-validator> <br/> <field-validator type = "stringlength"> <br/> <Param name = "minlength"> 4 </param> <br/> <Param name = "maxlength"> 32 </param> <br/> <message> the username length must be 4 up to 32 characters </message> <br/> </field-validator> <br/> </field> <br/> <field name = "password"> <br/> <field-validator type = "requiredstring"> <br/> <message> enter the password </message> <br/> </field-validator> <br/> <field-validator type = "stringlength"> <br/> <Param name = "minlength"> 6 </param> <br/> <Param name = "maxlength"> 32 </param> <br/> <message> for the security of your account, set a password with more than 6 letters (up to 32 letters can be set) </message> <br/> </field-validator> <br/> </field> <br/> </validators> <br/>In fact, the message information can be configured internationally: the configuration method is extremely simple, that is, <Message key = "userpass. required "> </message>. The key corresponds to the international information in the international configuration file. Struts2 has the following verification rules:

Required: required.
Requiredstring: Required string checker
INT: integer validator
Double: double-precision floating-point checker
Date: Date validator
Expression: expression validator
Fieldexpression: field expression validator
Email: email validator
URL: URL validator
Visitor: Visitor checker
Conversion: conversion validator
Stringlength: String Length checker
RegEx: Regular Expression validator


Common verification rules:

1. Mandatory check

<Validators> <field name = "username">
<Field-validator type = "required">
<Message> indicates a message indicating a failed verification. </message>
</Field-validator>
</Field>
</Validators>

2. Required string check

<Validators>
<Field name = "username">
<Field-validator type = "requiredstring">
<Param name = "trim"> true </param>
<Message> indicates a message indicating a failed verification. </message>
</Field-validator>
</Field>
</Validators>

3. Integer Inspector/floating point check

<Validators>
<Field name = "Age">
<Field-validator type = "int">
<Param name = "min"> 1 </param>
<Param name = "Max"> 150 </param>
<Message> the age must be between 1 and 150 </message>
</Field-validator>
</Field>
</Validators>

4. Date Inspection

<Validators>
<Field name = "birth">
<Field-validator type = "date">
<Param name = "min"> 1900-01-01 </param>
<Param name = "Max"> 2050-02-21 </param>
<Message key = "birth. Range"/>
</Field-validator>
</Field>
</Validators>

5. Field expression Tester (the specified field must meet a logic expression)

<Validators>
<Field name = "re_pass"> <field-validator type = "fieldexpression"> <! -- Specify the logical expression -->
<Param name = "expression"> (pass EQ re_pass) </param>
<Message> the password must be the same as the Confirmed password </message>
</Field-validator>
</Field>
</Validators>

6. Email address verification

<Validators>
<Field name = "email">
<Field-validator type = "email">
<Message> your email address must be a valid email address </message>
</Field-validator>
</Field>
</Validators>

7. URL
Inspection

<Validators>
<Field name = "url">
<Field-validator type = "url">
<Message> your home address must be a valid URL </message>
</Field-validator>
</Field>
</Validators>

8. String Length Test

<Validators>
<Field name = "user"> <field-validator type = "stringlength">
<Param name = "minlength"> 4 </param>
<Param name = "maxlength"> 20 </param>
<Message> your username must be in the range of 4 to 20 </message>
</Field-validator>
</Field>
</Validators>

9. Regular Expression Test

<Validators>
<Field name = "user">
<Field-validator type = "RegEx">
<Param name = "expression_r"> <! [CDATA [(\ W {4, 25})]> </param>
<Message> the user name you enter can only be a letter or array, and the length must be between 4 and 25 </message>
</Field-validator>
</Field>
</Validators> Next, let's take a simple logon verification example:
Login. jsp<% @ Page Language = "Java" Import = "Java. util. * "pageencoding =" UTF-8 "%> <br/> <% @ taglib uri ="/Struts-tags "prefix =" S "%> <br/> <% <br /> string Path = request. getcontextpath (); <br/> string basepath = request. getscheme () + ": //" <br/> + request. getservername () + ":" + request. getserverport () <br/> + path + "/"; <br/>%> <br/> <! Doctype HTML public "-// W3C // dtd html 4.01 transitional // en"> <br/> <HTML> <br/> <pead> <br/> <base href = "<% = basepath %>"> <br/> <title> my JSP 'fail. JSP 'starting page </title> <br/> <meta http-equiv = "Pragma" content = "no-Cache"> <br/> <meta http-equiv =" cache-control "content =" no-Cache "> <br/> <meta http-equiv =" expires "content =" 0 "> <br/> <meta http-equiv = "keywords" content = "keyword1, keyword2, keyword3 "> <br/> <Meta http-equiv = "Description" content = "this is my page"> <br/> </pead> <br/> <body> <br/> <s: form Action = "User/login. action "Validate =" true "> <br/> <Table <br/> style =" border-style: solid; border-color: lightblue; position: absolute; top: 30px "> <br/> <tbody> <br/> <tr> <br/> <TD> <br/> <Div <br/> style =" margin-center: 32px; border-style: solid; border-color: lightblue; border-width: 2px; "> <br/> <A> User name: </> <Br/> <input id = "username" name = "user. name "type =" text "/> <br/> </div> <br/> </TD> <br/> </tr> <br/> <tr> <br/> <TD> <br/> <Div <br/> style = "margin-center: 32px; border-style: solid; border-color: lightblue; border-width: 2px "> <br/> <A> password: </a> <br/> <input id = "password" name = "user. password "type =" password "/> <br/> </div> <br/> </TD> <br/> </tr> <br/> <! -- Error message popped up --> <br/> <tr> <br/> <TD> <br/> <Div id = "MSG"> <s: fielderror/> </div> <br/> </TD> <br/> </tr> <br/> <TD> <br/> <div> <br/> <span> <input id = "Submit" type = "Submit" value = "Logon"/> <br/> </span> <br /> </div> <br/> </TD> <br/> </tr> <br/> </tbody> <br/> </table> <br /> </s: form> <br/> </body> <br/> </ptml>Note that the validate = "true" attribute must be added to form. * ** Action-validation.xml <textarea cols="50" rows="15" name="code" class="c-sharp"><? XML version = "1.0" encoding = "UTF-8"?> <Br/> <! Doctype validators Public <br/> "-// opensymphony group // xwork validator 1.0 // en" <br/> "http://www.opensymphony.com/xwork/xwork-validator-1.0.2.dtd"> <br/> <validators> <br/> <field name = "user. name "> <br/> <field-validator type =" requiredstring "> <br/> <Message key =" username. required "> </message> <br/> </field-validator> <br/> </field> <br/> <field name =" user. password "> <br/> <field-validator type =" requiredstring "> <br/> <Message key =" userpass. required "> </message> <br/> </field-validator> <br/> </field> <br/> </validators></textarea> This method uses international information. * ** Action. action, which is quite simple, is the conventional method of action writing. It verifies the form in execute, or encapsulates the method verification form by itself, but cannot inherit the validate () method, because the validate method is implemented when the action is executed. If so, the configuration is repeated, but I didn't test any results (it doesn't make sense ). Struts. xml configuration <textarea cols="50" rows="15" name="code" class="c-sharp"><Constant name = "struts. custom. i18n. resources "value =" globalmessages "> </constant> <br/> <include file =" defaule. XML "/> <br/> <! -- User operations --> <br/> <package name = "user" extends = "struts-Default" namespace = "/user"> <br/> <action name = "login" class = "BBS. action. useraction "> <br/> <result name =" success ">/system/list. action </result> <br/> <result name = "input">/login. JSP </result> <br/> </Action> <br/> </package></textarea> It is necessary to mention that the result of input must be configured in the action, because if the verification fails, struts2 will automatically redirect to the result of input, no matter what result is configured for verification failure in your action. Therefore, the result of this INPUT attribute cannot be dropped. Effect: English language in IE:

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.