In the process of web development, we often need to fill out the form of the operation, we generally have to submit form information when the content of the form is verified , STRUTS2 provides us with a simple implementation interface, so that we can easily verify the form. The following is the most traditional way to configure a form validation framework , and more straightforward is to use annotations, which is not explained here.
1. First we want to create a new. xml file in the sibling directory of the action to be configured, named:actionclassname-validation.xml.
For example, my action class is named: Registeraction then the XML name is:registeraction-validation.xml
Special attention should be paid to:
1) Action to inherit Actionsupport
2) XML header: <! DOCTYPE validators Public "-//apache struts//xwork Validator 1.0.2//en" "http://struts.apache.org/dtds/ Xwork-validator-1.0.2.dtd ">
The contents are as follows:
<?xml version= "1.0" encoding= "UTF-8"? ><! DOCTYPE validators Public "-//apache struts//xwork Validator 1.0.2//en" "http://struts.apache.org/dtds/ Xwork-validator-1.0.2.dtd "><validators> <field name=" UserName "> <field-validator type= "Requiredstring" > <message> must enter user name </message> </field-validator> </field > <field name= "password" > <field-validator type= "Stringlength" > <param name= " MinLength ">6</param> <param name=" maxLength ">10</param> <message> Password length 6-10 </message> </field-validator> </field> <field name= "Validcode" > <field-validator type= "requiredstring" > <message> must enter verification code </message> </ Field-validator> </field></validators>
2. Configure Struts.xml
Configure success to jump after success
Configure input to jump after a validation failure
<action name= "register" class= "com.babybus.sdteam.action.RegisterAction" > <result name= "Success" > /success.jsp</result> <result name= "Input" >/Register.jsp</result>
3. Next is the JSP file
<s:form action= "Register" method= "POST" > <s:textfield name= "userName" label= "username"/> <s: TextField name= "password" label= "password"/> <s:textfield name= "Validcode" label= "Verification Code"/> <s:submit value= "register"/> </s:form>
4. Page results
Conclusion
- Benefit, familiarity with STRUTS2 validation framework
This site article is for baby bus SD. Team Original, reproduced must be clearly noted: (the author's official website: Baby bus )
Reprinted from "Baby bus Superdo Team" original link: http://www.cnblogs.com/superdo/p/4774011.html
[Javaweb Basics] 015.struts2 Form validation framework