Sunday, December 11, 2011

Struts-1 Basics

The Struts Framework is a standard for developing well-architected Web applications. It has the following features:
* Open source
* Based on the Model-View-Controller (MVC) design paradigm, distinctly separating all three levels:
o Model: application state
o View: presentation of data (JSP, HTML)
o Controller: routing of the application flow
* Implements the JSP Model 2 Architecture
* Stores application routing information and request mapping in a single core file, struts-config.xml

The Struts Framework, itself, only fills in the View and Controller layers. The Model layer is left to the developer.



All incoming requests are intercepted by the Struts servlet controller. The Struts Configuration file struts-config.xml is used by the controller to determine the routing of the flow. This flows consists of an alternation between two transitions:

** From View to Action :
A user clicks on a link or submits a form on an HTML or JSP page. The controller receives the request, looks up the mapping for this request, and forwards it to an action. The action in turn calls a Model layer (Business layer) service or function.

** From Action to View :
After the call to an underlying function or service returns to the action class, the action forwards to a resource in the View layer and a page is displayed in a web browser.

=====================
Struts Components
=====================

The Controller :
-----------------------
This receives all incoming requests. Its primary function is the mapping of a request URI to an action class selecting the proper application module. It's provided by the framework.
The struts-config.xml File

This file contains all of the routing and configuration information for the Struts application. This XML file needs to be in the WEB-INF directory of the application.
Action Classes

It's the developer's responsibility to create these classes. They act as bridges between user-invoked URIs and business services. Actions process a request and return an ActionForward object that identifies the next component to invoke. They're part of the Controller layer, not the Model layer.

View Resources
-------------------
View resources consist of Java Server Pages, HTML pages, JavaScript and Stylesheet files, Resource bundles, JavaBeans, and Struts JSP tags.

ActionForms
-------------------
These greatly simplify user form validation by capturing user data from the HTTP request. They act as a "firewall" between forms (Web pages) and the application (actions). These components allow the validation of user input before proceeding to an Action. If the input is invalid, a page with an error can be displayed.

Model Components
--------------------
The Struts Framework has no built-in support for the Model layer. Struts supports any model components:
* JavaBeans
* EJB
* CORBA
* JDO
* any other

=============
SEQUENCE FLOW
==============


The following events happen when the Client browser issues an HTTP request.

* The ActionServlet receives the request.
* The struts-config.xml file contains the details regarding the Actions, ActionForms, ActionMappings and ActionForwards.
* During the startup the ActionServelet reads the struts-config.xml file and creates a database of configuration objects. Later while processing the request the ActionServlet makes decision by refering to this object.

When the ActionServlet receives the request it does the following tasks :-

* Bundles all the request values into a JavaBean class which extends Struts ActionForm class.
* Decides which action class to invoke to process the request.
* Validate the data entered by the user.
* The action class process the request with the help of the model component. The model interacts with the database and process the request.
* After completing the request processing the Action class returns an ActionForward to the controller.
* Based on the ActionForward the controller will invoke the appropriate view.
* The HTTP response is rendered back to the user by the view component.

References:
-----------
Exadel Site
Site-2

No comments: