Introduction to Struts framework


Model-1 Architecture

In this Architecture all kinds of requirements get fulfilled in one layer. The user provides request to the jsp and jsp provides 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 gives request to the servlet present in the layer.

View: - This layer implements presentation logic of the application. It provides 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

Model: - This layer implements business logic and contains data. This can be created by using a set of java beans called as form bean. Struts provide a predefined class as the super class of all form beans.

Controller: - This layer implements controlling logic of the application. It takes 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 as form- action. Struts provide a predefined servlet as the controlling servlet .The controlling servlet receives request from the users. Struts provide a predefined class as the super class of all form action classes.

View: - This layer implements presentation logic. It provides response to the users. This layer can be created by using a set of Jsp. Struts provides 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 extracted folder and makes those files available in WEB-INF folder of the context.

  3. Search for all .jar files in 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 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 request.

<servlet>
<servlet-name>x</servlet-name>
<servlet-class>org.apache.struts.action.ActionServlet</servlet-class>
<init-param>
  <param-name>config</param-name>
  <param-value>/WEB-INF/struts-config.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
  <servlet-name>x</servlet-name>
<url-pattern>*.do</url-pattern>
</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 input of user and to provide appropriate forwards names to controlling servlets.

  4. Provide information in struts-config.xml.

  5. Create 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.Return type of this method must be org.apache.struts.ActionForward.This method accept 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 fom action files.

Providing information in struts-config.xml file

  1. provide information about formbean by using following tags in struts-config.xml as

<form-beans>
<form-bean name="name to map formbean with form action" type="class name with package name of the form bean" />
</form-beans>

  1. Supply information about form-action and forwards by using the following tags in struts-config.xml file

<action-mappings>
<action name="mapping name specified in formbean" path="value of action attribute present in from"  type="class name of form action" >
<forward name="name of forward used by form action" path="name of the page to be visible" />
<forward name=" name of forward used by form action " path=" name of the page to be visible " />
</action>
</action-mappings>

In the next section we will do a login page using struts framework... Stay tune...

Up Next
    Ebook Download
    View all
    Learn
    View all