Struts Tutorial -- Code ExamplesDynaValidatorForm
In this tutorial you will learn how to create Struts DynaValidatorForm. A regular ActionForm is developed in Java and declared in the struts_config. xml. The JavaBeans properties of a regular ActionForm are created by first defining the instance variable and then adding a getter and setter for that instance variable. A DynaValidatorForm has no associated Java class. Its JavaBeans properties are created by adding the <form-property> tag in Struts Config file.DynaValidatorForm is actually a subclass of DynaActionForm. It implements the validate() method much like the ValidatorForm and invokes the Commons Validator. DynaValidatorForm brings the capability of writing XML based validation rules.
For Example : you have a EmpForm and you don't want a java class (EmpForm).
EmpForm has propertis
firstName, lastName, country .
Step 1. Adding DynaValidatorForm Entry in struts-config.xml
Add the following entry in the struts-config.xml file. The <form-property/> tag is used to define the property for the form bean.
Step 2. Add action mapping in the struts-config.xml file:
Add the following action mapping in the struts-config.xml file:
Step 3. In the Action class.
Step 4. In the JSP page (empform.jsp)
Step 5. In the validation.xml - The validation.xml file is where you couple the individual Validators defined in the validator-rules.xml to components within your application
Step 6. In the validator-rules.xml - The validator-rules.xml file defines the Validator definitions available for a given application.
Step 8. In the the Resource Bundle. application_resource.properties file //Here you can add localization.
Now in the browser type http://localhost:8080/testApp/EmpForm.jsp
Don't Enter firstName and lastName in the text box and submit the "Save" BUTTON.
the RequestProcessor checks for the validateattribute in the
First Name is required.
In the empForm firstName and lastName are the required filed.
So in the above configuration you can see we add for both firstName and lastName.
You can see depends="required" - "required" property is defind in validator-rules.xml.
In the resource bundle : application_resource.propertis file
Two major differences between DynaActionForm and DynaValidatorForm
1) DynaValidatorForm is subclass of DynaActionForm.
2) A DynaActionForm has no associated Java class. Its JavaBeans properties are created by adding the <form-property> tag in Struts Config file. DynaValidatorForm also do the same with extra Validation with out writting code. DynaValidatorForm use XML to do validation.