Struts 2 Request processing

Struts 2 introduction
Struts 2 framework
Struts 2
A full-featured web application
framework for the Java EE
The Java Servlet API exposes
the HTTP client / server
protocol to the Java platform.
Struts 2 is built on top of the
Java Servlet API layer.
A framework makes
generalizations about the
common tasks and workflow of
a specific domain. It then
attempts to provide a platform
upon which applications of that
domain can be quickly built.
Struts 2
Java Servlet specification
History of Struts
Struts 2
Is from Apache software
 ASF is a non-profit corporation
(decentralized community of
developers) supporting Apache
software projects including the
Apache HTTP server.
Struts 2 is a brand new web
application framework
 It is not just a new release of
the Struts 1 framework, but a
completely new framework
based on the OpenSymphony
WebWork framework.
• Struts 2 implements the
popular Model-ViewController (MVC) design
• The MVC pattern provides
a separation of concerns
that applies well to web
• Separation of concerns
allows us to manage the
complexity of large
software systems by
dividing them into highlevel components
Struts 2 implementation of the MVC Pattern
Web browser
HTTP request
render page
invoke action
select result
Struts 2 MVC is realized by three core framework components : actions, results, and the
Struts 2 Request processing
invoke action
invoke result
ActionContext (ThreadLocal)
Struts 2 request processing uses interceptors that fire before and after the action and result
Declarative architecture [1]
Declarative architecture
Is a type of configuration
that allows developers to
describe their application
architecture (configure) at a
higher level than direct
programmatic manipulation
Configuration occurs at two
levels in Struts
 As the Struts framework is
flexible, it allows the
developer to tweak its
behaviour in many areas.
This activity is actually
configuring the behaviour
of the Struts framework
 The more important type of
configuration (or
declarative architecture)
refers to defining the Struts
2 components that are
used by your application
and wiring / linking them
together to form your
desired workflow paths
 Workflow paths refer to
which action fires when a
particular URL is hit, and
which results might be
chosen by that action to
complete processing.
Declarative architecture [2]
Multiple methods
There are two methods of
declaring the architecture of
the application being
 Through XML based
configuration files
 Through java annotations
Whether the declaration is
in XML or using java
annotations, the framework
translates them both into the
same run time components
 With XML, one has the
familiar XML configuration
document with elements
describing the application
actions, results and
 struts.xml
 With java annotations, the
XML is gone. The
metadata is located as java
annotations inside the java
source code for the classes
that implement the Struts 2
actions. An annotated
source file is shown below.
URL mapping to struts actions [helloworldxml
http:// + localhost:8080 + /helloworldxml + <package name space> + /HelloWorld.action
hostname:portnumber servlet context
package name space
action name.action
The URL combines the servlet context with the package namespace and the action
name. Note that the action name takes the .action extension. If the package name
space is specified, that should be used as part of the struts action identifier URL, else it
should be spaces.
Struts 2 data flow trace for the helloworldxml
Form rendered
by index.jsp
<s:textfield name="userName"
label="User Name"/>
<s:submit />
HelloWorld Action
Form rendered by success.jsp
<h1><s:property value="message"
String userName
String message
Struts 2 configuration
Configuration files
• The struts-config.xml
configuration file is a link
between the View and Model
components in the Web Client
but you would not have to
touch these settings for 99.99%
of your projects.
 Is a J2EE configuration file that
determines how the elements
of the HTTP request are
processed by the servlet
container. It is not a Struts 2
configuration file, but it is a file
that needs to be configured for
Struts to work
 Contains the configuration
information that developer will
be modifying as actions are
developed. This is created
under the directory WEBINF/classes.
• This configuration file provides
a mechanism to change the
default behaviour of the
framework. This file should be
created in the WEBINF/classes folder.Any line
starting with # in this file is
treated as comments.