Difference Betwixt Struts Validatorform Vs Validatoractionform - Interview Question

Struts render ii classes ValidatorForm together with ValidatorActionForm for validating cast fields. Instead of doing basic validation past times using the validate() method inwards Action class, these classes allows y'all to leverage rich characteristic of validation framework provided past times Djakarta Struts framework. You tin practice declarative validation past times defining rules inwards XML config files similar validation-rule.xml or validations.xml equally well. Since Struts is i of the pop Web MVC frameworks, y'all tin likewise purpose its validation framework on whatever Java spider web application implemented using Struts. Now, coming dorsum to the question, what is the actual deviation betwixt ValidatorForm together with ValidatorActionForm inwards Struts? They both await quite similar, right?

Well, fifty-fifty though both ValidatorForm together with ValidatorActionForm provides validation back upwards together with tin endure used to validate cast fields inwards Struts application, in that location is a subtle deviation betwixt them. The ValidatorForm cast uses the "form name" to perform validation, spell ValidatorActionForm cast uses the "action name" to perform validation inwards Struts.

There are clear benefits of each approach. When y'all purpose ValidatorForm together with then past times looking at the struts-config.xml file y'all tin clearly tell that a exceptional cast has next validations. But, that's non the representative when y'all purpose ValidatorActionForm, though it provides a to a greater extent than practical way to validate cast fields of a exceptional cast inwards Struts.

Sometimes it happens that a unmarried cast is shared amid many actions together with JSP pages, which agency configuring a unmarried validation for the cast would endure unnecessary. You volition waste product fourth dimension on validating fields which are non related to our page. H5N1 page tin endure distinguished using 'Action' hence past times using ValidatorActionForm y'all tin salve a lot of unnecessary validation.




Difference betwixt ValidatorForm together with ValidatorActionForm inwards Struts

Before looking at the deviation betwixt these ii classes, let's outset endeavor to revise to a greater extent than or less basics close Validator framework inwards Struts. The Validator Framework is used to render validation of cast fields e.g. y'all tin validate if the password entered inwards password champaign has minimum 8 grapheme or non together with if non together with then y'all tin automatically refuse submission together with demonstrate fault messages to the user.

The configuration of Validator Framework inwards Struts consist of ii XML files, validator-rules.xml together with validations.xml file. The outset i validator-rules.xml contains the default Struts pluggable validator definitions. You tin add together novel validation rules past times adding novel entries inwards 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 endure placed within the /WEB-INF/lib folder of the application.



Identifier

Thie outset of import deviation betwixt ValidatorForm together with ValidatorAction cast comes from the fact how they are identified. ValidatorForm uses "form name" equally identifier to invoke validation, spell ValidatorActionForm uses "action name" equally identifier to invoke validation


Configuration

In representative of ValidatorForm, the validations.xml together with struts-config.xml file volition endure configured equally 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 signal to Federal Reserve annotation is that cast scream should endure same inwards both validation.xml together with struts-config.xml file.

In representative of ValidationActionForm, the configuration inwards validator.xml together with struts-config.xml await 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 signal to Federal Reserve annotation is that scream attribute of cast tag contains the action path together with non the scream of the form-bean itself. If y'all desire to larn to a greater extent than close validator framework, y'all tin farther read Struts inwards Action, which covers it inwards adept details.

Example

Let's run across a uncomplicated login representative using the DynaValidatorForm. First, nosotros demand to practice 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 measuring is to add together validations to the cast fields inwards the validation.xml file. Our Form scream is "MyLoginForm" together with champaign names are "userName" together with "password".

 Struts render ii classes ValidatorForm together with ValidatorActionForm for validating cast fiel Difference betwixt Struts ValidatorForm vs ValidatorActionForm - Interview Question


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 inwards the previous paragraph that the scream attribute of cast tag should endure the scream of the cast y'all desire to associate alongside the Validation Framework. Name of the cast specified hither should likewise check alongside the i specified inwards the struts-config.xml file. Once y'all did this configuration,  you tin associate each belongings of the cast edible bean alongside i or to a greater extent than predefined validation rules.

Btw, If y'all desire to larn to a greater extent than close Struts 1.x together with then I propose y'all reading Programming Djakarta Struts, 2d Edition by Chuck Cavaness.

 Struts render ii classes ValidatorForm together with ValidatorActionForm for validating cast fiel Difference betwixt Struts ValidatorForm vs ValidatorActionForm - Interview Question


In the representative of ValidatorActionForm, since validations are tied upwards alongside activeness path together with non alongside form, it's possible to reuse the same cast for dissimilar validations depending upon activeness path equally shown below:

&lt;form-validation&gt;     &lt;formset&gt;         &lt;!-- validation mapping for action 1 --&gt;         &lt;form name="/myAction1"&gt;             ...         &lt;/form&gt;     &lt;/formset&gt;     &lt;formset&gt;         &lt;!-- validation mapping for activeness 2 --&gt;         &lt;form name="/myAction2"&gt;             ...         &lt;/form&gt;     &lt;/formset&gt; &lt;/form-validation&gt;

This gives y'all to a greater extent than flexibility inwards damage of code reuse together with specifying validations based upon requirement.

This is likewise i of the good Struts interview questions, equally it allows y'all to banking firm check if the candidate has used Struts validator framework or not. Let's run across a span of to a greater extent than points to sympathise the deviation better. I convey seen this inquiry many times on Java spider web developer interviewer where task description mentioned close Struts. Don't retrieve Spring MVC has completely killed the Struts framework, similar Java Swing, in that location are a lot of existent globe projection even hence exists in Struts 1.x and Struts 2.x and that's why Java developer alongside cognition of Struts framework are ever inwards adept demand.


That's all close the difference betwixt ValidatorForm together with ValidatorActionForm inwards Struts framework. It's meliorate to purpose ValidatorForm because it's clear to run across which cast has validations, only sometimes it happens that a unmarried cast is used past times multiple Action classes inwards JSP pages. In this case, configuring a unmarried validator for the cast would endure overkill. Since the validation rules of a ValidatorActionForm are non assigned to an activeness cast only to the activeness (/path-to-action.do). So it is likewise possible to reuse a edible bean inwards multiple actions only 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



Sumber https://javarevisited.blogspot.com/

0 Response to "Difference Betwixt Struts Validatorform Vs Validatoractionform - Interview Question"

Post a Comment

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel