Difference Betwixt Struts Validatorform Vs Validatoractionform - Interview Question
Struts render ii classes ValidatorForm as well as ValidatorActionForm for validating shape fields. Instead of doing basic validation past times using the validate() method inward Action class, these classes allows yous to leverage rich characteristic of validation framework provided past times Djakarta Struts framework. You tin exercise declarative validation past times defining rules inward XML config files similar validation-rule.xml or validations.xml every bit well. Since Struts is i of the pop Web MVC frameworks, yous tin likewise role its validation framework on whatever Java spider web application implemented using Struts. Now, coming dorsum to the question, what is the actual divergence betwixt ValidatorForm as well as ValidatorActionForm inward Struts? They both expect quite similar, right?
Well, fifty-fifty though both ValidatorForm as well as ValidatorActionForm provides validation back upward as well as tin live on used to validate shape fields inward Struts application, in that location is a subtle divergence betwixt them. The ValidatorForm degree uses the "form name" to perform validation, spell ValidatorActionForm degree uses the "action name" to perform validation inward Struts.
There are clear benefits of each approach. When yous role ValidatorForm as well as therefore past times looking at the struts-config.xml file yous tin clearly tell that a exceptional shape has next validations. But, that's non the instance when yous role ValidatorActionForm, though it provides a to a greater extent than practical way to validate shape fields of a exceptional shape inward Struts.
Sometimes it happens that a unmarried shape is shared with many actions as well as JSP pages, which agency configuring a unmarried validation for the shape would live on unnecessary. You volition waste materials fourth dimension on validating fields which are non related to our page. Influenza A virus subtype H5N1 page tin live on distinguished using 'Action' therefore past times using ValidatorActionForm yous tin relieve a lot of unnecessary validation.
The configuration of Validator Framework inward Struts consist of ii XML files, validator-rules.xml as well as validations.xml file. The kickoff i validator-rules.xml contains the default Struts pluggable validator definitions. You tin add together novel validation rules past times adding novel entries inward this file.
The instant i validation.xml file contains details regarding the validation routines that are applied to the dissimilar Form Beans. These ii configuration files should live on placed within the /WEB-INF/lib folder of the application.
Validations.xml
struts-config.xml
In instance of ValidationActionForm, the configuration inward validator.xml as well as struts-config.xml expect similar below:
Validations.xml
struts-config.xml
The key betoken to banker's complaint is that holler attribute of shape tag contains the action path as well as non the holler of the form-bean itself. If yous desire to larn to a greater extent than most validator framework, yous tin farther read Struts inward Action, which covers it inward skilful details.
Next mensuration is to add together validations to the shape fields inward the validation.xml file. Our Form holler is "MyLoginForm" as well as acre names are "userName" as well as "password".
The validation.xml file contains the next code.
As I said inward the previous paragraph that the holler attribute of shape tag should live on the holler of the shape yous desire to associate with the Validation Framework. Name of the shape specified hither should likewise tally with the i specified inward the struts-config.xml file. Once yous did this configuration, you tin associate each belongings of the shape edible bean with i or to a greater extent than predefined validation rules.
Btw, If yous desire to larn to a greater extent than most Struts 1.x as well as therefore I propose yous reading Programming Djakarta Struts, 2d Edition by Chuck Cavaness.
In the instance of ValidatorActionForm, since validations are tied upward with activeness path as well as non with form, it's possible to reuse the same shape for dissimilar validations depending upon activeness path every bit shown below:
This gives yous to a greater extent than flexibility inward price of code reuse as well as specifying validations based upon requirement.
This is likewise i of the good Struts interview questions, every bit it allows yous to banking venture check if the candidate has used Struts validator framework or not. Let's meet a span of to a greater extent than points to empathize the divergence better. I bring seen this inquiry many times on Java spider web developer interviewer where chore description mentioned most Struts. Don't retrieve Spring MVC has completely killed the Struts framework, similar Java Swing, in that location are a lot of existent the world projection however exists in Struts 1.x and Struts 2.x and that's why Java developer with noesis of Struts framework are e'er inward skilful demand.
That's all most the difference betwixt ValidatorForm as well as ValidatorActionForm inward Struts framework. It's amend to role ValidatorForm because it's clear to meet which shape has validations, exactly sometimes it happens that a unmarried shape is used past times multiple Action classes inward JSP pages. In this case, configuring a unmarried validator for the shape would live on overkill. Since the validation rules of a ValidatorActionForm are non assigned to an activeness shape exactly to the activeness (/path-to-action.do). So it is likewise possible to reuse a edible bean inward multiple actions exactly to define dissimilar validations for each action.
Further Learning
Java Web Fundamentals By Kevin Jones
Struts 2 Framework for Beginners
Spring Framework Master Class - Beginner to Expert
Well, fifty-fifty though both ValidatorForm as well as ValidatorActionForm provides validation back upward as well as tin live on used to validate shape fields inward Struts application, in that location is a subtle divergence betwixt them. The ValidatorForm degree uses the "form name" to perform validation, spell ValidatorActionForm degree uses the "action name" to perform validation inward Struts.
There are clear benefits of each approach. When yous role ValidatorForm as well as therefore past times looking at the struts-config.xml file yous tin clearly tell that a exceptional shape has next validations. But, that's non the instance when yous role ValidatorActionForm, though it provides a to a greater extent than practical way to validate shape fields of a exceptional shape inward Struts.
Sometimes it happens that a unmarried shape is shared with many actions as well as JSP pages, which agency configuring a unmarried validation for the shape would live on unnecessary. You volition waste materials fourth dimension on validating fields which are non related to our page. Influenza A virus subtype H5N1 page tin live on distinguished using 'Action' therefore past times using ValidatorActionForm yous tin relieve a lot of unnecessary validation.
Difference betwixt ValidatorForm as well as ValidatorActionForm inward Struts
Before looking at the divergence betwixt these ii classes, let's kickoff seek to revise to a greater extent than or less basics most Validator framework inward Struts. The Validator Framework is used to render validation of shape fields e.g. yous tin validate if the password entered inward password acre has minimum 8 graphic symbol or non as well as if non as well as therefore yous tin automatically decline submission as well as present fault messages to the user.The configuration of Validator Framework inward Struts consist of ii XML files, validator-rules.xml as well as validations.xml file. The kickoff i validator-rules.xml contains the default Struts pluggable validator definitions. You tin add together novel validation rules past times adding novel entries inward this file.
The instant i validation.xml file contains details regarding the validation routines that are applied to the dissimilar Form Beans. These ii configuration files should live on placed within the /WEB-INF/lib folder of the application.
Identifier
Thie kickoff of import divergence betwixt ValidatorForm as well as ValidatorAction shape comes from the fact how they are identified. ValidatorForm uses "form name" every bit identifier to invoke validation, spell ValidatorActionForm uses "action name" every bit identifier to invoke validationConfiguration
In instance of ValidatorForm, the validations.xml as well as struts-config.xml file volition live on configured every bit 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 betoken to banker's complaint is that shape holler should live on same inward both validation.xml as well as struts-config.xml file.
In instance of ValidationActionForm, the configuration inward validator.xml as well as struts-config.xml expect similar 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 betoken to banker's complaint is that holler attribute of shape tag contains the action path as well as non the holler of the form-bean itself. If yous desire to larn to a greater extent than most validator framework, yous tin farther read Struts inward Action, which covers it inward skilful details.
Example
Let's meet a uncomplicated login representative using the DynaValidatorForm. First, nosotros take away to exercise 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 mensuration is to add together validations to the shape fields inward the validation.xml file. Our Form holler is "MyLoginForm" as well as acre names are "userName" as well as "password".
The validation.xml file contains the next 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 inward the previous paragraph that the holler attribute of shape tag should live on the holler of the shape yous desire to associate with the Validation Framework. Name of the shape specified hither should likewise tally with the i specified inward the struts-config.xml file. Once yous did this configuration, you tin associate each belongings of the shape edible bean with i or to a greater extent than predefined validation rules.
Btw, If yous desire to larn to a greater extent than most Struts 1.x as well as therefore I propose yous reading Programming Djakarta Struts, 2d Edition by Chuck Cavaness.
In the instance of ValidatorActionForm, since validations are tied upward with activeness path as well as non with form, it's possible to reuse the same shape for dissimilar validations depending upon activeness path every bit shown below:
<form-validation> <formset> <!-- validation mapping for action 1 --> <form name="/myAction1"> ... </form> </formset> <formset> <!-- validation mapping for activeness 2 --> <form name="/myAction2"> ... </form> </formset> </form-validation>
This gives yous to a greater extent than flexibility inward price of code reuse as well as specifying validations based upon requirement.
This is likewise i of the good Struts interview questions, every bit it allows yous to banking venture check if the candidate has used Struts validator framework or not. Let's meet a span of to a greater extent than points to empathize the divergence better. I bring seen this inquiry many times on Java spider web developer interviewer where chore description mentioned most Struts. Don't retrieve Spring MVC has completely killed the Struts framework, similar Java Swing, in that location are a lot of existent the world projection however exists in Struts 1.x and Struts 2.x and that's why Java developer with noesis of Struts framework are e'er inward skilful demand.
That's all most the difference betwixt ValidatorForm as well as ValidatorActionForm inward Struts framework. It's amend to role ValidatorForm because it's clear to meet which shape has validations, exactly sometimes it happens that a unmarried shape is used past times multiple Action classes inward JSP pages. In this case, configuring a unmarried validator for the shape would live on overkill. Since the validation rules of a ValidatorActionForm are non assigned to an activeness shape exactly to the activeness (/path-to-action.do). So it is likewise possible to reuse a edible bean inward multiple actions exactly to define dissimilar validations for each action.
Further Learning
Java Web Fundamentals By Kevin Jones
Struts 2 Framework for Beginners
Spring Framework Master Class - Beginner to Expert


Komentar
Posting Komentar