Struts validation rules

Source: Internet
Author: User

The principle of the Struts validation rule:

To implement validation, you need to add a method to the action, such as the Validate () method you want to override. Suppose there is a method for register in action. The default interceptor for struts is executed before the Register method is executed <interceptor name= "validation" class= " Org.apache.struts2.interceptor.validation.AnnotationValidationInterceptor "/> The bottom of this interceptor maintains a map collection, Add the error message to this map and save it when the validation is not passed. Then go to input view to prompt.

1. Code to validate validators for all methods in the action

This action must inherit the Actionsupport class: Because the interceptor needs to be used.

Rewrite the Validate () method.

@Overridepublic void Validate () {//user name non-empty if (User.getusername () ==null| | "". Equals (User.getusername ())) {Super.addfielderror ("username", "user name cannot be empty");} if (User.getpassword () ==null| | User.getpassword (). Length () <8) {super.addfielderror ("password", "Password cannot be empty, and the length is greater than 8 bits");}}
2. Code method validator specified in action

Name specification validate+ the method name that needs to be validated, such as Validateregister () then this validator is only valid for the Register method

Verifies that the specified method public void Validateregister () {//user name is non-empty if (User.getusername () ==null| | "". Equals (User.getusername ())) {Super.addfielderror ("username", "user name cannot be empty");} if (User.getpassword () ==null| | User.getpassword (). Length () <8) {super.addfielderror ("password", "Password cannot be empty, and the length is greater than 8 bits");}}
3.xml validates all methods in the action

XML naming: Actionclassname+validation.xml: Useraction-validation.xml

This XML needs to be placed under a directory similar to the action class.

<?xml version= "1.0" encoding= "UTF-8"?> <! DOCTYPE validators Public "-//apache struts//xwork Validator 1.0.3//en" "http://struts.apache.org/dtds/ Xwork-validator-1.0.3.dtd "> <validators> <field name=" User.username "> <field-validator type=" Requiredstring "> <message> username cannot be empty! </message> </field-validator> </field> <field name= "User.password" > <field-validator type = "requiredstring" > <message> password cannot be empty! </message> </field-validator> <field-validator type= "Stringlength" > <param name= "minlength" >6</param> <param name= "maxlength" >8</param> <message> password at 6 to 8 bits </message> </ field-validator> </field> <field name= "User.date" > <field-validator type= "Date" > <message > Date format is incorrect! </message> </field-validator> </field> <field name= "User.email" > <field-validator type= " Email > <message> email format is incorrect! </message> </field-validaTor> </field> </validators> 
4.xml validates the method specified in the Action class

XML naming conventions: Actionclassname-actonname-validation.xml such as Useraction-user_register-validation.xml

The XML configuration is the same as 3.

5. Run the configuration:
Struts.xml red font represents configuration global error view

<struts><package name= "user" extends= "struts-default" ><span style= "color: #ff0000;" ><global-results><result name= "Input" >/register.jsp</result></global-results></ Span><action name= "user_*" class= "cn.itcast.action.UserAction" method= "{1}" ><result name= "register" >/index.jsp</result><result name= "List" >/index.jsp</result></action></package> </struts>
Register.jsp the location of the red font displays an error message

<body>   <form action= "${pagecontext.request.contextpath}/user_register" method= "POST" >   < Span style= "color: #ff0000;" ><s:fielderror></s:fielderror></span>   Username: <input type= "text"  name= " User.username "/><br>   Password: <input type=" text "name=" User.password "/><br>   onboarding Date:< Input type= "text" name= "User.date"/><br>   e-mail: <input type= "text" name= "User.email" ><br>   <input type= "Submit" value= "Submission"/>   </form>   </body>




Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Struts 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.