Struts 2 default XML validation tutorial


          

            In this tutorial we'll cover how to validate a user's input in form fields using Struts 2's XML validation methodology. In the Form Validation tutorial we discussed validating a user's input using a separate XML validation file gives you the ability to use validators built-in to the Struts 2 framework.

        Create a xml file in the same directory where you controller are placed using the name conversion ControllerName-validation.xml

        Example if your controller name is LoginController.java then your xml name should be LoginController-validation.xml


Code

<!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="uname">
    <field-validator type="requiredstring">
       <message>User Name is required.</message>
    </field-validator>
</field>

<field name="pass">
    <field-validator type="requiredstring">
       <message>Password is required.</message>
    </field-validator>
</field>


<field name="mail">
    <field-validator type="requiredstring">
    <message>email is required</message>
    </field-validator>

    <field-validator type="email">
    <message>enter valid email id</message>
    </field-validator>
</field>

</validators>


Screenshot

        

        


Download

        Struts 2 xml vaildation war

        Struts 2 xml vaildation maven zip




Related Post

Comments


©candidjava.com