Different between ForwardAction and ActionForward

ActionForward

ForwardAction

  1. Represents the result pages of struts Action class.

 

  1. Use forward tags in s-c.xml file to configure action forwards.
 1. A built-in Action class using which the resource of struts application can comm.. with each other through controller servlet called as ActionServlet.2. Use action tag in s-c.xml file to configure ForwardAction class as separate Action class.

Struts supplied <html: link> is alternate for the traditional <a> tag to prepare hyperlinks.

Example:-  <a href = “home.do”> tryagain </a>(or)

<html: link href = “home.do”> Tryagain </html: link>

<html: link action = “home.do”> Tryagain </html: link>

Here .do word is the extension word of ActionServlet url pattern (*.do)

Question: can we develop Struts application without form bean class (both manual & dynamic)?

Answer:    If Struts Action class is getting request from hyperlink of web page without data. Then form bean is not required.

  • If Struts Actoin class is getting request from form page and there is no need of perfering form validations, reset (-,-) method operations then the form bean can be mode as optional resource in the situation the execute method of StrutsAction class read the form data of form page through third parameter of execute () method i.e request object.

Different Types of Action Classes / Built in Action Classes

Different types of Action classes / Built in Action classes: –

  • SwitchAction
  • Forwordaction
  • Local Action
  • Include Action
  • Dispatch Action
  • Lookup Dispatch Action
  • Mapping Dispatch Action
  • Download Action
  • These classes are available in org.apache.Struts.Action package.
  • This package is available in Struts-home\lib\Stuts-extras-1.3.8.jar file.
  • Foll all these action classes the derect / indirect super class is org.apache.Struts.action.Action class.

 Different types of Action classes

A.jsp:-

<a href = “B.jsp”> go </a>

  • A.jsp is directly comm. with B.jsp. so we can say the diagram is violating MVC2 principle which says the two jsps of view layer must communicate each other through controller servlet.

Solution (1):-

Different types of Action classes Built in Action Classes

A.jsp:-

<a href = “xyz.do”> go </a>

S-c.xml

    <action path = “/xyz” type = “xyzAction”>

         <forward name = “ok” path = “IB.jsp”/>

</action>

  • This diagram satisfies MVC2 principles towards hyperlink based communication b/w A.jsp and B.jsp that means A.jsp is communicating with B.jsp through the controller servlet called ActionServlet. But taking one separate Action class having only mapping.findForword () call in execute method to satisfy the requirement is meaningless and not recommended aperation.

Sulution (2):- (with forward action) 

  • In this diagram A.jsp is comm… With B.jsp through controller servlet. So we can say MVC2 principle is not violated more ever it is not taking support of any dummy action class to full fill the requirement.

With respect to diagram (1):-

1)      Enduser click on the go hyperlink of A.jsp

2)      Based on bref url the request url will be generated.

3)      ActionServlet traps & takes the request

4)      ActionServlet uses Struts-configuration file to get the TargetAction class

5)      ActionServlet finds forwardAction class as the Target Action class to execute based on the Acti action path “/xyz”

6)      The predefined execute (-,-) method of predefined forward Action class uses the parameter attribute value B.jsp and returns ActionForward object to ActionServlet pointing to B.jsp

7)      ActionServlet forwards the control to B.jsp

  • While working with all built-in Actionclasses including ForwordAction we must add Struts-home\lib\ Struts-extracts-1.3.8.jar file to WEB-INF\lib folder of your Struts application.
  • In MVC2 Arh. Based applications all the operations in the web application execution must take place under the control of controller servlet for better monitoring controller on the application execution.

Procedure to work with StrutsDemoApp1 application with programmiticDynamicFormBean

Procedure to work with StrutsDemoApp1 application with programmiticDynamicFormBean:

Step1:- Develop FormBean class as shown below manually

RegisterForm.java

Package app;

Important org.apache.Struts.action.*;

Important org.apache.Struts.validator.*;

Important java.servlet.http.*;

Public class RegisterForm extends dynaValidatorForm

{

//no formbean properties

// no setxxx (-,-) & getxxx (-,-)

Public ActionErrors validate (-,-)

{

// call super.validate (-,-) to perform validation plug-in based form validations

ActionErrors errs = super.validate (mapping, req);

// read form data from form bean properties

String user = (String) get (“username”);

String pwd = (String) get (“password”);

// develop custom form validation logic (programatic)

// check whether user & pwd are having same length or not

If (errors.size () == 0)

{

Errors .add (“unpwderr”, new ActionMessage (“my.unpwd.len”))

}

}

S.o.pln (“size of error is” + errs.size ());

Return errs;

} // validate (-,-)

}

To compile this source file add commons-beanutil-1.7.0.jar file in class path as addition jar file.

Step2:- configure the above form bean class in struts configuration filr along with form properties.

In Struts-config.xml file:-

<s-c>

<form-beans>

<form-bean name = “rf” type = “app.RegisterForm”>

< form-property name = “username” type = “java.long.String” initial = “ramu”/>

< form-property name = “password” type = “java.long.String” />

</form-bean>

</form-beans>

Step3:– write following code is RegisterAction.java to read form data from form bean class object.

Same as step(3) of working with declarative dynamic form bean [ref.previous scanerio] but write type casting stmt as show below.
Register fm = (RegisterForm) form;

Step4: – keep the remaining resource as it is.

  • Both DynaActionForm & DynaValidatorForm can generated FormBeans dynamically but DynaActionForm cannot work with validator plug-in where as DynaVakidatorForm can work with validator plug-in.
  • For complote example application that uses programaticDynamic bean and all the 3 appraoches to mixup our form validation logic with validator plug-in refer supplymentory handout given on 25/8/11
  • Example Application that uses maximum validator rules of validator plug-in along with dynamic FormBean utilization (Declarative):-
  • Nve- no form validation errors
  • Ve- validation Errors are there.
  • W.r.to above diagram (5)? indicates that ActionServlet perform validator plug-in based form validations on form bean data based on the entries placed in validation.xml and validator-rules.xml file.
  • For the above diagram based application refer application (4) of the booklet page no. 26-30.
  • When form page having multiple components then it is recommended to develop context in that form page as html table context to make to alignment form easily in the web page.

Question: why Application Parameter is removed from struts 1.3 to configure properties file?

Answer:    This application init parameter allows to configure only one property as a time. So it is recommends to use <message-resource> tag of <s-c>.xml file to configure multiple properties files….

  • Once java.sql.Data class object is given to jdbc driver it is a responsibility of jdbc driver to insert date value in the table column is the pattern i.e supported by underlying database software.

Converting string date value to java.sql.date class object when given date value pattern is yyyy-mm-dd.

String s = “2012-4-24”;

Java.sql.date sqdl = java.sql.date.valueOf (s);

  • Converting string date value to java.sql.date class object when given date value pattern is any pattern.

String s = “10-10-1983”;  // mm-dd-yyyy

//get java.util.date object

simpleDateFormat sdfl = new simple DateFormat (“mm-dd-yyyy”);

java.util.Date udl = sdfl.parse(s);

//get java.sql.Date object

Long ms = udl.get Time ()

Java.sql.Date sqd2 = new java.sql.Date (ms);

Application with Declarative Dynamic Form Bean

Procedure to work with StrutsDemoApp1-DV application with declarative dynamic form bean:

Step1:- delete RegisterForm.java & its class file form the application

Step2:- modify existing form bean configuration in struts configuration file as shown below.

Step3:- Write following code in the execute (-) method of RefisterAction class to read values from form bean class object.

In RegisterAction.java:-

 

Step3:- keep remaing resource as it is.

Limitation with Declarative Dynamic FormBean:-

1)      There is no provision to add reset (-,-) method to solve problems related to session scoped form bean related check box, list box components.

2)      There is no provision to keep validate (-,-) method and to use approach no. (2) Of adding custom validation logics to validator plug-in.

  • To solve both these problems develop DynamicFormBean as programiticDynamicFormBean.

Dynamic Form Beans in Struts – Struts Tutorial

Dynamic Form Beans in Struts:

  • If form bean class is developed by the programmer completely & totally then it is called as manual form bean class / explicit form bean class. The form bean class that we have developed so far comes under explicit form beans.
  • If framework software generates total form bean or atleast form bean properties and their getxxx () and setxxx () methods dynamically. Then they are called as “dynamic form beans”.
  • To work with Dynamic form beans we need to use DynaxxxForm classes like:-
    • DynaActionForm
    • DynaValidatorForm
    • DynaValidatorActionForm

Six important form bean types:-

ActionForm

  • DynaActionForm (o.a.s action)

DynaValidatorForm ValidatorActionForm (o.a.s Validator)

DynaValidatorActionForm (o.a.s Validator)

  •  ValidatorForm (o.a.s Validator)

Validator ActionForm  (o.a.s Validator)

Dynamic Form Beans:

1) Declarative Dynamic FormBean

  • Total FormBean will be generated dynamically
  • Cfg DynaxxxForm class in Struts-cfg file explicitly                                                                                   

2)  Programatic DynamicFormBean

  • Needs to write FormBean class explicitly
  • The formbean properties and getxxx(), setxxx () will be generated dynamically
  • Programmatic dynamic form bean are good to use

Apply Mask Validator Rule on Form Bean Property

In validation.xml to apply mask validator rule on form bean property (user name):-

In my file properties

 

More Regular Expressions for form validation:-

Limitation with mask validator rule:-

  1. This validator rule is not working on specific form components like text area and etc.(working properly with textbox & problem is there with client side javascript execution)
  2. Programmer cannot write the form validation logic of mask validator rule by using multiple form bean properties data that means the following form validation logic are not possible with mask validator.

a)      Checking whether password & confirm passwords are matching or not.

b)      Checking whether username & password are beginning with same character or not and etc.

  1. Working with Regular Expression takes time to habituate.
  • Procedure to modify the port number of glassfish & s/w that comes with Net beans IDE installation (for domain 1 server):—-

– Go to Glassfish-home\AppServer\domain1\config\folder à open domain.xml file

-Modify port attribute value of <http-listener> tag available in line n0. 61

  • To solve the problems of approach no.(1) (mask validation rule) use approach no.(2) calling super.validate() from our validate (-) method of form bean class and calling validator plug-in generated dynamic javascript function from user defined javascript function of form page.

Note:-  Our validate () of form bean class performs programmatic server side form validations.

  • Our javascript function of form page performs programmatic client side form validations.
  • The validator plug-in generated dynamic javascript function performs declarative client side form validations.
  • Approach no. (2) is very much recomanded to write programmatic form validation logic based on the data of multiple form bean properties.
  • Procedure to perform formvalidations based on approach no. (2) to mix-up our form validation logic with the validator plug-in based form valadition at client side to serverside (with respect to StrutsDemoApp1-DV application).

Step1:– override validate () in our form bean class calling super.validate () and also by having programmatic server side form validation ligic.

In registerForm, java

 

 

Step2: add additional form validation error message in properties file.

     In myfile.properties

          My.unped.len = username & password must have same length.

Step3: – Add user defined javascript function in form page having client side programmatic validation logic and also calling the validator plug-in generated dynamic javascript function.

In register.jsp:-

Step4:– develop the remaining resources of the application manually.

  • The mask validator rule (approach no. (1)) is specific to one form bean property on which it is applied.
  • The approach no. (2) form validation logic is specific to single form page/ single form bean class.
  • To solve approach (1) & approach (2) limitations use approach (3) which allows to add custom validator rule in vaidator plug-in.

Note: – Validator rule of the validator plug-in is visible in all form pages/ form bean classes of Struts application.

Approach no. (3):- adding custom vaidator rule can be defined having both java & javascript code to perform client side & server side form validations. This validator rule can use only one form bean property data at a time to perform form validation use approach (3) to give Struts app. Level global visibility to your form validation logic.

  • Procedure to add custom vaidator rule to validator plug-in of (checking whether first character & last character of form bean property value) is same or not:-

Step (1):- Define default error message for custom vaidator rule in properties file. (With respect

                  StrutsDemoApp1-DV)

In myfile.properties:-

My.custrule.msg = {0} must contain same first & last character.

Step2:-configure cust validator rule in validator-rules.xml file having logical name

In WEB-INF\ validator-rules.xml:- 

Step5:-  add satya validateSatyaRule.js file in WEB-INF\classes folder having custom form validation logic for SatyaRule. (javascript code).   

            ValidateSatyaRule.js (develop this file based on other.js file of commons validator-1.3.1.jar file)

Step6:- apply this SatyaRule on username property of form bean class.

In validation.xml:-

 

Step7:- develop the remaining resources of the applications in regular manner.

Note: – We can apply all the 3 approaches in single application to mixup our form validation logic’s with the validator plug-in supplied predefined validator rules.

Configuring User-Defined Error Messages for Required, Maxlength Validator Rules

Configuring user-defined error messages for required, maxlength validator rules:-

In validation, xml:-

    <field property = “username” depends = “reauired”>

       <msg name = “required” key = “my.un.msg”/>

     </field> 

<field property = “password” depends = “reauired, maxlength”>

       <msg name = “required” key = “my.pass.msg”/>

      <msg name = “maxlength” key = “my.pass.max.msg”/>

<arg position = “1” key = “$ {var: maxlength}” resource = “false”/>

<var>

  <var-name> maxlength </var-name>

   <var-value> 10 </var-value>

</var>

  1. Supplying user defined error msg’s for required, maxlength valildator rules.
  2. Supplying input values required for maxlength validator rule.

In myfile.properties

  My.un.msg = username is mandatory

  My.pass.msg = Hey…….enter password

  My.pass.max.msg = u r password is too large (max of 10)

Note: – This property file need not maintain default error messages.

  • The above process of configuring user defined error msg’s for validator rules is not a recommended process bcz it kills the reusability of error messages.
  • During the testing phase of the project if you want to disable form validations temporarily then we can use validate = “false” attribute in action tag. This makes ActionServlet not to call & validate () method of form bean class in form bean life cycle. The value of this attribute in “true”.

Example: – <action-mapping>

         <action path = “register” type = “app.RegisterAction” name = “rf” input = “/register.jsp” validate = “false”>

  • This validate = “false” can disable path programmatic & declarative form validation of server side, but cannot disable programmatic & declarative form validation of client side(javascript)
  • The ActionForm class based form bean class of Struts application cannot be used for validator plug-in based server side form validation but it can be used the validator  plug-in based client side form validations.(java script)
  • Once validator plug-in is recognized by ActionServlet then the javascript code of validator rules becomes ready to come to browser window when <html: javascript> tag is executable, irrespective of whether setup is ready or not perform validator plug-in based server side form validation.
  • Since there are only 14+ validator rules in validate plug-in so they are not really sufficient for real time project development, to overcome this problem the programmer must know the process of mixing our form validation logics with validator plug-in supplied validator rules. This can be done in three approaches.
  1. By using mask validator rule
  2. By calling super.validate () method from validate () of our form bean class.

By calling valifator plug-in generated javascript function from our java script function of form page.

  1. By adding user defined validator rule in validator plug-in.

Approach (1): working with the validator rule

  • Mask is a predefined validator rule of validator plug-in that allows the programmer to write regular expression based custom form validation logic in validation.xml file.
  • Errors.invalid key holds default error msg of mask validator rule. This can be modified by using msg tag.
Errors.invalid = {0} is invalid.                    
  • Every regular expression begins with ‘^’ (char & symbol) and ends with “$“ (dollar symbol)
  • The regular expression simplifies the process of writing complex & length java code, javascript code based logics.(form validation logics)

Performance of Validator Plugin in Server Side Form Validation

Question:  How validator plug-in is performing server side form validation in our Struts-apps (explain flow)

  • Form page submits the request to struts applications
  • ActionServlet traps and takes the request and also reads the form data of form page
  • ActionServlet writes formdata to form bean class properties of form bean class object by calling setxxx () method
  • ActionServlet calls validate () method of our form bean class object since it is not available the super class validate () method will be execute
  • This super class validate () method activator plug-in (validate form class)
  • This validator plug-in uses the entries of validation.xml file to know the validator rules that are applied on form bean properties.
  • Validator plug-in performs form validation on form bean properties data

                 Validation errors are there                                                     No-Validation errors

  • Super class validate (-,-) returns                                    Super class validate (-,-) returns Action-Errors
  • Action-Error class object with elements (size>0)         class object without elements (size = 0).

ActionServlet forward the control to input.jsp program to display validation error messages.

When <arg> tag is used with validator rule name then that <arg> tag supplies argument value of only that validator rule related error message.

  •        <field properties = “password” depends = “required, maxlength”>
  •          <arg position = “0” name = “required” key = “my.pass”/> -> Supplies {0} valiue for required rule related form validation error messages.
  •          <arg position = “0” name = “maxlength” key = “my.pass1”/> -> Supplies {0} valiue for maxlength rule related form validation error messages.
  •             —————-
  •              —————–
  •       </field>

When <arg> tag is used without validator rule name then that tag can supply argument value for multiple values rule form validation error messages.

Example:- 

  • <field properties = “password” depends = “required,maxlength”>
  •                 <arg position = “0” key = “my.pass”/>
  •                         ————-
  •                         ————-
  •  </field>

In the above code my.pass supplies {0} value for required, maxlength validator rules related error messages.

  • Code in validation.xml to apply required, maxlength validation rules on password form bean property.

In validation.xml

  •   <field property = “password” depends = “reauired, maxlength”>
  •        <arg position = “0” name = “required” key = “my.pass”/>
  •         <arg position = “0” name = “maxlength” key = “my.pass1”/>
  •         <arg position = “1” name = “maxlength” key = “$ {var: maxlength}” resource = “false”/>
  •       <var>
  •        <var-name> maxlength </var-name>
  •         <var-value> 10 </var-value>
  •        </var>
  •    </field>
  • If you have not liked the default error msg of validator rules then we can configure user defined messages as form validation error messages by using <msg> tag in validation.xml file. In this situation if any other than {0} value is required for validator rules as input values then they must be supplied by using <arg> tag.