In this post , you will learn what form beans is, what its importance is, how to use form beans in struts application, and then the validation mechanisms to validate the struts form fields like programmatic and declarative, validation at server side and cliet side. First we will start with what form beans is and what is its importance, the following section describes what form beans is.

Form Bean

Form Bean in struts framework is a simple java bean class that is a subtype of org.apache.struts.action.ActionForm class. Form Beans provides a proper mechanism to exchange the data between the view and actions with reduced dependency i.e this serves as a transfer object pattern some times known as Data Transfer Object(DTO). A part from this form beans separates the form data conversions and validations from actions, which reduces the complexity of the actions and provides a proper responsibility division. Note: It is not recommended to use form beans as a Transfer Object for transferring the data between the controller and Model since form beans are dependent on struts API and it is not better practice to increase dependency in such a way.

Need of Form Beans

Most of the Actions designed in struts applications are required to process the form data and in all such an actions if we try to obtain form data using getParameter() method of ServletRequest, we will face the following problems

 Ø  Dependency between the view & actions increases
 Ø  Data conversions has to be performed from actions , which increases the complexity of the action classes, affects the understandability of the action classes and if the same conversions are required with different forms data then either we  need to implement them independently or need to develop out own library.
 Ø  Affects the productivity
 Ø  If any validations are required for the form fields , they have to be performed in action class, which once again increases the complexity

Benefits of Form Beans

 Ø  Form Beans provides the following benefits:
 Ø  Separates the form data conversions and validations from actions
 Ø  Serves as a transfer object  between the view and controller i.e this follows Transfer Object pattern some times known as Data Transfer Object(DTO)
  
Using Form bean in Struts application:

Using form beans in struts application is a simple two-step operation, first write a form bean class and then configure the form bean to work with the struts application, the following section describes about these two operation in detail.

Writing a Struts Form bean class

The regular form bean class is a java class that satisfies the following rules:

 Ø  The form bean class should be a public non-abstract class
 Ø  The form bean class should be a subtype of org.apache.struts.action.ActionForm
 Ø  Form bean class should have no-argument constructor ,we are allowed to have multiple constructors as per our applications requirement
 Ø  The form bean class should have a set and get method for each of the property that it has to represented, where these set and get methods should be public as written according to the java bean standards.
 Ø  The form bean class can optionally override reset()and validate() methods of Action Form, you will learn more about reset() and validate() methods in detail in the next post.
The following code snippet shows a regular form bean class that is designed to represent a login form with two properties, uname of type java.lang.String and pass of type java.lang.String.




Package com.chandra.struts;
Import org.apache.struts .action.ActionForm;
Import org.apache.struts.action.ActionMapping;
Import org.apache.struts.action.ActionErrors;
Import org.spache.struts.action.ActionError;
Import javax.servlet.http.HttpServletRequest;
Public class LoginForm extends ActionForm
{
            Public LoginForm(){}
            Public LoginForm(String uname,String pass)
            {
               this.uname=uname;
               this.pass=pass;
            }
Public void setUname(String uname){this.uname=uname;}
Public String getUname(){return uname;}
Public void setPass(String pass){this.pass=pass;}
Public String getPass(){return pass;}
String uname,pass;
//optionally we can override validate() and reset() methods
}

The reset() method

The reset() method of form bean provides a mechanism to reset the form properties between action requests. The reset() method is invoked before the form properties are repopulated, thus this method can be considered as an initialization method per request for the bean. The method signature of reset() method is shown in the snippet shown bellow
Public void reset(ActionMapping mapping, ServletRequest request)
The reset method does not have any significance for the form bean objects that are configure to request scope. The default implementation of this method given in org.apache.struts.acton.ActionForm class does nothing i.e the method implementation is empty.

The validate() method

The validate() method of form bean provides a mechanism to validate the form properties before the form bean object is submitted to action. The validate() method is invoked after the form properties are populated and before the action is invoked. The validate() method is invoked olu of the validate attribute of <action> element in struts configuration file is set to true. The complete method signature of validate() method is shown in the snippet shown below.

Public ActionErrors validate(ActionMapping mapping,HttpServletRequest request)

As shown in the above code snippet the validate() methods return type is an ActionErrors. The ActionErrors encapsulates ActionMessage objects that describe any validation errors that have been found. In case if the validate() method returns null or an Actionerrors object with no recorded error messages i.e an empty ActionErrors, then it describes the controller that there are no errors in validating the form fields, the default implementation for this method in ActionForm return null. However, if an Actionerrors with even a single error describes the controller that there are failures in validating the form fields, in this case the request is not delegated to Action instead it is dispatched to the input page configured in the struts configuration file you will learn this configurations in the next post.


Post a Comment

 
Top