Introduction to Struts framework

Model-1 Architecture

 
In this Architecture, all kinds of requirements get fulfilled in one layer. The user provides the request to the JSP and JSP provides a response to the user. This architecture uses a set of JSP to implement presentation logic, business logic, controlling logic. It is suitable for small scale requirement due to its simplicity. It does not provide reusability.
 

Model-2 Architecture

 
This layer is divided into three parts they are as below
  1. Model
  2. Controller
  3. View
  • Model
    This layer implements business logic and contains data. State of users can be stored in this layer. This layer can be created by using a set of java beans.
  • Controller
    This layer implements controlling logic. It takes decision to make visible appropriate view to the appropriate users. This layer can be created by using a servlet and a set of helper classes. The users give request to the servlet present in the layer.
  • View
    This layer implements the presentation logic of the application. It provides a response to the user. A set of JSP can be used to create this layer.

Introduction to Struts

 
This is a framework to implement MVC Architecture in a java based web application. It uses three different layers to implements different types of logic. It provides a set of classes and tag libraries for being used in different layers of the application.
 

MVC Architecture of struts

 
1. Model: - This layer implements business logic and contains data. This can be created by using a set of java beans called a form bean. Struts provide a predefined class as the super class of all form beans.
 
2. Controller: - This layer implements the controlling logic of the application. It takes the decision to make visible appropriate view to appropriate users. This can be created by using a controlling servlet and a set of helper class called form- action. Struts provide a predefined servlet as the controlling servlet. The controlling servlet receives a request from the users. Struts provide a predefined class as the super class of all form action classes.
 
3. View: - This layer implements presentation logic. It provides a response to the users. This layer can be created by using a set of JSP. Struts provide tag libraries for this layer.
 

Installation of struts

  1. Get the copy of struts binary distribution (Struts 1.3.8-all.zip) from http://struts.apache.org/download.cgi and extract it into any folder of the file system.
  2. Search for all .tld files in the extracted folder and makes those files available in WEB-INF folder of the context.
  3. Search for all .jar files in the extracted folder and make those files available in lib folder of the context.

Configuration of struts

 
Create a configuration file named as struts-config.xml inside WEB-INF folder. This file contains information about form-bean, form-action, and forwards.
 
Tips: -
  1. Search for struts-config.xml inside the extracted folder of struts. Copy that file to make available in WEB-INF folder of the context. Open this file and remove all the tag present between <struts-config></struts-config>
  2. Deploy the controlling servlet by using the required in WEB-INF. Name of the controlling servlet is org.apache.struts.action.Actionservlet. This servlet must accept the name of the configuration file by using an initial parameter named as config. This servlet must be a permanent servlet. This servlet must have an url-pattern to receive all types of request.
  1. <servlet>  
  2. <servlet-name>x</servlet-name>  
  3. <servlet-class>org.apache.struts.action.ActionServlet</servlet-class>  
  4. <init-param>  
  5.   <param-name>config</param-name>  
  6.   <param-value>/WEB-INF/struts-config.xml</param-value>  
  7. </init-param>  
  8. <load-on-startup>1</load-on-startup>  
  9. </servlet>  
  10. <servlet-mapping>  
  11.   <servlet-name>x</servlet-name>  
  12. <url-pattern>*.do</url-pattern>  
  13. </servlet-mapping>  

Creating a form-based application in struts

 

Requirements

  1. Create the JSP to contain the form.
  2. Create a form-bean to contain values of form fields.
  3. Create a form-action to validate the input of the user and to provide appropriate forwards names to controlling servlets.
  4. Provide information in struts-config.xml.
  5. Create a page for each forwards.

Creation of form bean

  1. Create a class in a package by inheriting org.apache.struts.action.ActionForm.
  2. Provide variables in the form-bean class as the properties. Name of these variables must match with the value of property attributes present in form field tags.
  3. Provide setter and getter methods for each of the property.
  4. Optionally override reset () method to provide default values to the properties.

Creation of form action

  1. Create a class in the same package where form-bean exists and this class must inherit org.apache.struts.action.Action class. 
  2. Override execute () method to implement validation logic and to return a forward. The return type of this method must be org.apache.struts.ActionForward.This method accepts four parameters as follows below.

    a. org.apache.struts.action.Actionmappings
    This represents all forward name and present in struts-config.xml and helps to create object of actionforward.
    b. org.apache.struts.action.Actionform
    This represent form bean.
    c. javax.servlet.http.HttpServletRequest
    This represent  user request.
    d. javax.servlet.http.HttpServletResponse
    This represent service response.

Compilation of bean and action


Compile formbean and action file simultaneously by using servlet-api.jar and struts-core-1.3.8.jar in classpath. The jar file of struts(struts-core-1.3.8.jar) can be found in lib folder of the context, after installation of structs.
 
--- \classes\logs\javac - classpath servlet-api.jar ;struts-core-1.3.8.jar *.java
 
Here logs is the package folder where we stored both the form bean and form action files.
 
Providing information in struts-config.xml file
  1. provide information about formbean by using the following tags in struts-config.xml as
    1. <form-beans>  
    2. <form-bean name="name to map formbean with form action" type="class name with package name of the form bean" />  
    3. </form-beans>  
  1. Supply information about form-action and forwards by using the following tags in struts-config.xml file
    1. <action-mappings>  
    2. <action name="mapping name specified in formbean" path="value of action attribute present in from"  type="class name of form action" >  
    3. <forward name="name of forward used by form action" path="name of the page to be visible" />  
    4. <forward name=" name of forward used by form action " path=" name of the page to be visible " />  
    5. </action>  
    6. </action-mappings>  
In the next section, we will do a login page using struts framework... Stay tuned...