Struts Framework Overview
(Web Tutorial)
by Michael Thomas
Struts Home Page
last updated on 1/21/06
Terms - My Notes
- Struts Framework - an Apache project that contains a set of open
source frameworks and toolkits that help developers develop web applications
based on the "Model 2" application architecture.
- Types of Struts Frameworks - On Jan 06 struts had 2
frameworks.
- Struts Action Framework - the original framework based on JSP
(Java Server Pages).
- Struts Shale Framework - a framework based on JSF (Java
Server Faces).
- Model 2 architecture - a servlet (or equivalent) manages the
business layer and controller layer with the presentation layer handled by
server pages (ex: JSP or JSF).
- Java Bean - a java class that stores variables which hold data.
- Presentation layer - web pages that are viewed by web clients.
These pages are usually created by the server processing JSP or JSF server
pages that display data passed by the Controller layer which got the data
from the Business layer. JSP and JSF pages create web pages that run
on web browsers (ie: html, javascript, etc...). The Presentation layer
usually sends it's data to the Controller layer which then passes the data
to the Business layer.
- Business layer - application programs that run on the server.
These programs process the data gathered by the Presentation layer and then
pass data (Java Beans) to the Controller layer which may pass the data onto
the Presentation layer or another Business layer.
- Controller layer - a servlet (or equivalent) that runs on the
server and controls which action is called next based on the URL called by
the Presentation layer or a value passed to the Controller layer from the
Business layer. The Controller layer gets it's control rules from the
struts-config.xml. The Controller layer passes data via Java Beans to
the Business Layer or the Presentation Layer. The Presentation layer
can pass data to the Controller layer via a HTML form in which the data is
then converted to a Java Bean that is then passed to a Business layer.
The Business layer then creates an ActionMapping object and a Java Bean of
data, if it wants to pass on data, to the Controller layer that then passes
the Java Bean a Presentation Layer or another Business Layer based on the
value in the ActionMapping object.
- High Level Walkthrough Example - if this information seems to
technical, I suggest that you complete my steps to create your first Struts
web app and then re-read this section. I think it's a good idea to
read this information before and after completing the "Struts - First Steps"
link on my Struts home page ( Struts Framework ).
- Presentation layer posts a form to a URL. The
Presentation layer posts a HTML form to the Controller layer. The
Controller layer then takes the data in the form and creates a Java
Bean. Next the Controller layer looks at the URL value and
determines, via an xml config file, which Business layer (an action) to
pass the Java Bean to. The Business layer process the data and
then creates an ActionMapping object that will tell the Controller layer
which action to call next. The Business layer may also create a
Java Bean of data to pass on to the Controller layer. The
Controller layer looks at the ActionMapping object and determines, via
an xml config file, which action to call next. The Controller
layer then passes the Java Bean (if there is one) to the action which
could be another Business layer but is usually a Presentation layer.
Let's assume that the next action is a Presentation layer. The
Presentation layer then creates a web page based on the Java Beans
passed to the layer.
- Struts and Presentation data - All presentation data
should come from a properties file and/or a data source (DB etc...).
A properties file is used so that an application can be multi-lingual (ie:
English, Spanish, French, etc...). The data source will have an
user specific stored data (ie: any account data etc...).
- Validation using the server side ValidatorPlugIn - The
struts-config file contains a plug-in tag for the ValidatorPlugIn that
points to an xml file that contains the validation rules. So let's
discuss a walk through of the Presentation layer posting to the
Controller layer (via a .do file in the URL) that uses the server side
ValidatorPlugIn. The Controller layer takes the data in the Form
and creates a Java Bean. If the Controller layer sees a struts
action tag that has a property "name" that references a form-bean which
has a property "type" that reference the DynaValidatorForm class then
the Controller layer passes the Java Bean, created from the HTML form,
to the DynaValidatorForm class. If validation passes, then the
DynaValidatorForm class tells the Controller layer to continue to the
next action. If validation fails, then the DynaValidatorForm
creates an ActionMessages object with the error information and passes
that to the Controller layer. The Controller layer then passes the
error object (a Java Bean) to the action specified in the action tags
"input" property which is usually the Presentation layer that did the
original posting. The Presentation layer then notices that a Error
object was passed to it so it then displays the error information which
instructs the user to correct the invalid data and then click "Submit"
again.
Apache Resources
- Apache Struts Framework Home Page -
http://struts.apache.org/
- Apache Software Foundation -
www.apache.org - the Apache Software Foundation home page. A
community of open-source software projects, users, and developers.