Struts provide two classes ValidatorForm and ValidatorActionForm for validating form fields. Instead of doing basic validation by using the validate() method in Action class, these classes allows you to leverage rich feature of validation framework provided by Jakarta Struts framework. You can do declarative validation by defining rules in XML config files like validation-rule.xml or validations.xml as well. Since Struts is one of the popular Web MVC frameworks, you can also use its validation framework on any Java web application implemented using Struts. Now, coming back to the question, what is the actual difference between ValidatorForm and ValidatorActionForm in Struts? They both look quite similar, right?
Well, even though both ValidatorForm and ValidatorActionForm provides validation support and can be used to validate form fields in Struts application, there is a subtle difference between them. The ValidatorForm class uses the "form name" to perform validation, while ValidatorActionForm class uses the "action name" to perform validation in Struts.
There are clear benefits of each approach. When you use ValidatorForm then by looking at the struts-config.xml file you can clearly say that a particular form has following validations. But, that's not the case when you use ValidatorActionForm, though it provides a more practical way to validate form fields of a particular form in Struts.
Sometimes it happens that a single form is shared among many actions and JSP pages, which means configuring a single validation for the form would be unnecessary. You will waste time on validating fields which are not related to our page. A page can be distinguished using 'Action' so by using ValidatorActionForm you can save a lot of unnecessary validation.
The configuration of Validator Framework in Struts consist of two XML files, validator-rules.xml and validations.xml file. The first one validator-rules.xml contains the default Struts pluggable validator definitions. You can add new validation rules by adding new entries in this file.
The second one validation.xml file contains details regarding the validation routines that are applied to the different Form Beans. These two configuration files should be placed inside the /WEB-INF/lib folder of the application.
Validations.xml
struts-config.xml
In case of ValidationActionForm, the configuration in validator.xml and struts-config.xml look like below:
Validations.xml
struts-config.xml
The key point to note is that name attribute of form tag contains the action path and not the name of the form-bean itself. If you want to learn more about validator framework, you can further read Struts in Action, which covers it in good details.
Next step is to add validations to the form fields in the validation.xml file. Our Form name is "MyLoginForm" and field names are "userName" and "password".
The validation.xml file contains the following code.
As I said in the previous paragraph that the name attribute of form tag should be the name of the form you want to associate with the Validation Framework. Name of the form specified here should also match with the one specified in the struts-config.xml file. Once you did this configuration, you can associate each property of the form bean with one or more predefined validation rules.
Btw, If you want to learn more about Struts 1.x then I suggest you reading Programming Jakarta Struts, 2nd Edition by Chuck Cavaness.
In the case of ValidatorActionForm, since validations are tied up with action path and not with form, it's possible to reuse the same form for different validations depending upon action path as shown below:
This gives you more flexibility in terms of code reuse and specifying validations based upon requirement.
This is also one of the good Struts interview questions, as it allows you to check if the candidate has used Struts validator framework or not. Let's see a couple of more points to understand the difference better. I have seen this question many times on the Java web developer interviewer where job description mentioned about Struts.
Well, even though both ValidatorForm and ValidatorActionForm provides validation support and can be used to validate form fields in Struts application, there is a subtle difference between them. The ValidatorForm class uses the "form name" to perform validation, while ValidatorActionForm class uses the "action name" to perform validation in Struts.
There are clear benefits of each approach. When you use ValidatorForm then by looking at the struts-config.xml file you can clearly say that a particular form has following validations. But, that's not the case when you use ValidatorActionForm, though it provides a more practical way to validate form fields of a particular form in Struts.
Sometimes it happens that a single form is shared among many actions and JSP pages, which means configuring a single validation for the form would be unnecessary. You will waste time on validating fields which are not related to our page. A page can be distinguished using 'Action' so by using ValidatorActionForm you can save a lot of unnecessary validation.
Difference between ValidatorForm and ValidatorActionForm in Struts
Before looking at the difference between these two classes, let's first try to revise some basics about Validator framework in Struts. The Validator Framework is used to provide validation of form fields e.g. you can validate if the password entered in password field has minimum 8 character or not and if not then you can automatically reject submission and show error messages to the user.The configuration of Validator Framework in Struts consist of two XML files, validator-rules.xml and validations.xml file. The first one validator-rules.xml contains the default Struts pluggable validator definitions. You can add new validation rules by adding new entries in this file.
The second one validation.xml file contains details regarding the validation routines that are applied to the different Form Beans. These two configuration files should be placed inside the /WEB-INF/lib folder of the application.
Identifier
Thie first important difference between ValidatorForm and ValidatorAction form comes from the fact how they are identified. ValidatorForm uses "form name" as identifier to invoke validation, while ValidatorActionForm uses "action name" as identifier to invoke validationConfiguration
In case of ValidatorForm, the validations.xml and struts-config.xml file will be configured as following:Validations.xml
<form-validation> <formset> <form name="myForm"> <field property="userName" depends="required"> <arg key="MyLoginForm.userName"/> </field> <field property="password" depends="required,minlength"> <arg0 key="MyLoginForm.password"/> <arg1 key="${var:minlength}" name="minlength" resource="false"/> <var> <var-name>minlength</var-name> <var-value>6</var-value> </var> </field> </form> </formset> </form-validation>
struts-config.xml
<form-beans> <form-bean name="myForm" type="....."/> </form-beans> <action-mapping> <action path="/myFormAction" name= "myForm" type=……… />The key point to note is that form name should be same in both validation.xml and struts-config.xml file.
In case of ValidationActionForm, the configuration in validator.xml and struts-config.xml look like below:
Validations.xml
<formset> <form-name=”/myFormAction”> ...... ....... </form-name> <formset>
struts-config.xml
<form-beans> <form-bean name="myForm" type="......."/> </form-beans> <action-mapping> <action path="/myFormAction" name="myForm" type=……… />
The key point to note is that name attribute of form tag contains the action path and not the name of the form-bean itself. If you want to learn more about validator framework, you can further read Struts in Action, which covers it in good details.
Example
Let's see a simple login example using the DynaValidatorForm. First, we need to create a From Bean that extends org.apache.struts.validator.DynaValidatorForm.<form-beans> <form-bean name="MyLoginForm" type="org.apache.struts.validator.DynaValidatorForm"> <form-property name="userName" type="java.lang.String" /> <form-property name="password" type="java.lang.String" /> </form-bean> </form-beans>
Next step is to add validations to the form fields in the validation.xml file. Our Form name is "MyLoginForm" and field names are "userName" and "password".
The validation.xml file contains the following code.
<form-validation> <formset> <form name="MyLoginForm"> <field property="userName" depends="required"> <arg key="MyLoginForm.userName"/> </field> <field property="password" depends="required,minlength"> <arg0 key="MyLoginForm.password"/> <arg1 key="${var:minlength}" name="minlength" resource="false"/> <var> <var-name>minlength</var-name> <var-value>8</var-value> </var> </field> </form> </formset> </form-validation>
As I said in the previous paragraph that the name attribute of form tag should be the name of the form you want to associate with the Validation Framework. Name of the form specified here should also match with the one specified in the struts-config.xml file. Once you did this configuration, you can associate each property of the form bean with one or more predefined validation rules.
Btw, If you want to learn more about Struts 1.x then I suggest you reading Programming Jakarta Struts, 2nd Edition by Chuck Cavaness.
In the case of ValidatorActionForm, since validations are tied up with action path and not with form, it's possible to reuse the same form for different validations depending upon action path as shown below:
<form-validation> <formset> <!-- validation mapping for action 1 --> <form name="/myAction1"> ... </form> </formset> <formset> <!-- validation mapping for action 2 --> <form name="/myAction2"> ... </form> </formset> </form-validation>
This gives you more flexibility in terms of code reuse and specifying validations based upon requirement.
This is also one of the good Struts interview questions, as it allows you to check if the candidate has used Struts validator framework or not. Let's see a couple of more points to understand the difference better. I have seen this question many times on the Java web developer interviewer where job description mentioned about Struts.
Don't think Spring MVC has completely killed the Struts framework, like Java Swing, there are a lot of real world project still exists in Struts 1.x and Struts 2.x and that's why Java developer with knowledge of Struts framework are always in good demand.
That's all about the difference between ValidatorForm and ValidatorActionForm in Struts framework. It's better to use ValidatorForm because it's clear to see which form has validations, but sometimes it happens that a single form is used by multiple Action classes in JSP pages. In this case, configuring a single validator for the form would be overkill. Since the validation rules of a ValidatorActionForm are not assigned to an action form but to the action (/path-to-action.do). So it is also possible to reuse a bean in multiple actions but to define different validations for each action.
That's all about the difference between ValidatorForm and ValidatorActionForm in Struts framework. It's better to use ValidatorForm because it's clear to see which form has validations, but sometimes it happens that a single form is used by multiple Action classes in JSP pages. In this case, configuring a single validator for the form would be overkill. Since the validation rules of a ValidatorActionForm are not assigned to an action form but to the action (/path-to-action.do). So it is also possible to reuse a bean in multiple actions but to define different validations for each action.
No comments :
Post a Comment