Struts 2 - Framework Overview
(Web Tutorial)
by Michael Thomas
Struts Home Page
In this tutorial an overview of the Struts 2 Framework will be
discussed.
Note: On the "Struts Home Page", you can download
the whole site (all content, tutorials & examples) !!!
This tutorial covers: Struts 2
Prerequisites
Objectives
Struts 2 Architecture
The Struts official web site says: "Apache Struts is a free open-source
framework for creating Java web applications. ... The Struts framework is
designed to help developers create web applications that utilize a MVC
architecture." (http://struts.apache.org)
Apache Struts 2 - Quote from Struts Docs (after install: docs\index.html)
"Apache Struts 2 is an elegant, extensible framework for creating
enterprise-ready Java web applications. The framework is designed to
streamline the full development cycle, from building, to deploying, to
maintaining applications over time." (Struts Docs)
"Apache Struts 2 was originally known as WebWork 2. After working
independently for several years, the WebWork and Struts communities joined
forces to create Struts2. This new version of Struts is simpler to use and
closer to how Struts was always meant to be." (Struts Docs)
MVC (Model-VIew-Controller) is an software development architecture pattern.
The MVC (Model-VIew-Controller) architecture separates software applications
into 3 concerns (layers):
- Model (business layer) - business or database code
- View (presentation layer) - page design code
- Controller (navigation layer)- navigational code
MVC advocates believe it is easier to create and maintain larger applications
when the above concerns are separated. Development, testing and
maintenance of code can be separated in to the 3 concerns allowing multiple
skill sets to be used (Model-Java & DB, View-Struts2). Modules can be
rearranged easier based on actions in the Controller layer.
Old JSP (Java Server Pages) Framework: JSP Model 1 - framework
which was JSP page controlled.
- Framework design (View
a figure from Javaworld from this
webpage.)
- Client: Browser (Request & Response comes from JSP page)
- Application Server
- View & Controller - JSP (HTML & Java code) - has processing
logic.
- A URL accessed a .JSP page that accessed Java Beans to get
data, massage the data, display and decided where to go next
(navigation).
ex: http://localhost:8080/mystruts2-helloworld-final/mystrutshelloworld/Welcome.jsp
- Model - Java Beans to access data and process.
- Data Sources
New JSP Framework: JSP Model 2 (MVC) - page designers can work
in the View layer while Java developers can work in the Controller & Model
layers.
- Framework design (View
a figure from Javaworld from this
webpage.)
- Client: Browser (Request & Response comes from JSP page)
- Application Server
- View - JSP (HTML) - no processing logic
- Controller - Servlet for navigation.
- A URL calls an action.
ex: http://localhost:8080/mystruts2-helloworld-final/mystrutshelloworld/Welcome.action
- Model - Java Beans to access data and process.
- Data Sources
Struts Implementation of MVC
- View - JSP pages (no Java, HTML and an Expression Language (EL))
EL for Struts 1 - JSTL (JavaServer Pages Standard Tag Library) (Tag
Example: <jsp:include>)
EL for Struts 2 - OGNL (Object-Graph Navigation Language). (Tag
example: <s:include>
- Controller - XML - struts.xml - servlet reads xml file for
control/navigation mapping.
- Model - java files (Java Beans, Java Action Beans, etc...)
Terminology
- JSP Model 1 - is not an MVC pattern.
- JSP Model 2 - is a MVC pattern.
- JSP Model 2 is sometimes referred to as "MVC Model 2" or "MVC2" because
it is an implementation of "JSP Model 2". That can be confusing
because there is no MVC1 (nonexistent
term). So I do not like using the terms "MVC Model 2" or "MVC2"
because there is no "MVC Model 1" or
"MVC1".
Struts Guidlines (Rules)
- No Java code in JSPs. All business logic resides in Java classes
called action classes.
- Use Struts tags to access the business model.
Struts 1 vs Struts 2
- Struts 1 & Struts 2 is a very different implementation of MVC.
Struts 2 comes from the WebWork 2 project.
- Custom Tags: Struts 1 - custom tags were difficult. Struts 2 - custom
tags are easier.
- Extension: Struts 1 - the default extension is ".do". Struts 2 - the
default extension is ".action" (but ".do" will also work).
- Expression Language (presentation tags): Struts 1 uses JSTL (JSTL EL). Struts 2 uses OGNL (Object-Graph Navigation Language) but can use JSTL also.
...
Struts History
- Apache Struts 2 is a product of Apache Struts adopting the
OpenSymphony WebWork project. Struts 2 is not an extension of Struts 1
(or Classic Struts) but an extension of WebWork..
- Struts 1 & 2 - based on MVC architecture.
- Struts 1 - July 2001 first released.
- Struts 2 - Feb 2007 first released.
Resources