[Javaweb Basics] 015.struts2 Form validation framework

Source: Internet
Author: User

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

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.