Overview of Struts
- Struts is
open source implementation of
Java MVC (Model-View-Controller) pattern for the development of web
applications on JEE plateform
- The core of
the Struts framework is a flexible
control layer based on standard technologies like Java Servlets,
ResourceBundles, and XML).
encourages application architectures based on the
Model 2 approach, a variation of the classic Model-View-Controller
provides its own Controller component and
integrates with other technologies to provide the Model and the View
- Struts can
interact with standard data access
technologies, like JDBC and EJB, as well as most any third-party
Hibernate, iBATIS, or Object Relational Bridge.
For the View,
Struts works well with JavaServer Pages,
including JSTL and JSF, as well as Velocity Templates, XSLT, and other
1 design Pattern
- The presentation logic usually leads to a significant
amount of Java code embedded in the JSP in the form of scriptlets
making it ugly and maintenance nightmare even for experienced Java
- In large applications, JSPs are developed and
maintained by page authors. The intermingled scriptlets and markup
results in unclear definition of roles and is very problematic.
- Application control is decentralized since the next
page to be displayed is determined by the logic embedded in the current
page. Decentralized navigation control can cause headaches.
MVC suggests that an application should have three parts
Represents system state and business logic
beans. E.g java beans and enterprise beans
View portion represents the user interface and for a J2EE web
application is most often constructed using JavaServer Pages (JSP)
The Controller is focused on receiving
requests from the client (typically a user running a web browser),
deciding what business logic function is to be performed, and then
delegating responsibility for producing the next phase of the user
interface to an appropriate View component
2 Architecture - MVC
- The Controller Servlet handles the user’s request.
(This means the hyperlink in the JSP should point to the controller
- The Controller Servlet then instantiates appropriate
JavaBeans based on the request parameters (and optionally also based on
- The Controller Servlet then by itself or through a
controller helper communicates with the middle tier or directly to the
database to fetch the required data.
- The Controller sets the resultant JavaBeans (either
same or a new one) in one of the following contexts – request, session
- The controller then dispatches the request to the
next view based on the request URL.
- The View uses the resultant JavaBeans from Step 4 to
- Since there is no presentation logic in JSP, there
are no scriptlets.
- Web applications based on Model 2 architecture are
easier to maintain and extend since the views do not refer to each
other and there is no presentation logic in the views.
- It also allows you to clearly define the roles and
responsibilities in large projects thus allowing better coordination
among team members.
- 3 Major Components in STRUTS
- Servlet controller (Controller)
- Java Server Pages (View)
- Application Business Logic (Model)
- Controller bundles and routes HTTP request to other
objects in framework
Controller parses configuration file.
- Configuration file contains action mappings
- Controller uses mappings to turn HTTP requests into
- Mapping must specify
- A request path
- Object type to act upon the request
How the Struts Framework
- In Struts, there is only one controller servlet for
the entire web application.
This controller servlet is called ActionServlet.
- The servlet intercepts every client request and
populates an ActionForm
from the HTTP request parameters.
is a normal JavaBeans class. It has several attributes corresponding to
the HTTP request parameters and getter, setter methods for those
attributes. You have to create your own ActionForm for every
HTTP request handled through the Struts framework by extending the
- They are essentially View Data Transfer Object.
Data Transfer Object is an object that holds the data from html page
and transfers it around in the web tier framework and application
- The ActionServlet then instantiates a Handler.
- The Handler class name is obtained from an XML file
based on the URL path information.
- This XML file is referred to as Struts
configuration file and by default named as struts-config.xml.
Handler is called Action
in the Struts terminology
- The Action class is abstract and defines a single
method called execute().
You override this method in your own Actionsand invoke the business
logic in this method.
- The execute()method returns the name of next view
(JSP) to be shown to the user.
- The ActionServlet forwards to the selected view.
All residing in org.apache.struts.action package.
- The central component of the Struts Controller is the
- Extends the javax.servlet.HttpServlet.
- On startup, its reads the Struts Configuration file
and loads it into memory in the init() method.
- In the doGet() and doPost() methods, it intercepts
HTTP request and handles it appropriately.
The web.xml entry
for configuring the
ActionServlet and Struts
file is as follows
- ActionServlet intercepts HTTP requests based on the
URL pattern and handles them appropriately. The URL pattern can be
either path or suffix.
- This is specified using the servlet-mapping
- An example of suffix mapping is as follows.
And Action Mapping
- Once the ActionServlet intercepts the HTTP request,
it doesn’t do much.
- It delegates the request handling to another class
by invoking its process() method.
- The RequestProcessor first retrieves appropriate XML
block for the URL from struts-config.xml.
- This XML block is referred to as ActionMapping.
- In fact there is a class called ActionMapping.It
holds the mapping between a URL and Ac
<forward name="failure" path="Failure.jsp" />
- The RequestProcessor looks up the configuration file
for the URL pattern
- /submitDetailForm. (i.e. URL path without the suffix
- The type attribute tells Struts which Action class
has to be instantiated.
- The above ActionMapping tells Struts to map the URL
request with the path /submitDetailForm to the class
- After selecting ActionMapping, the RequestProcessor
instantiates the ActionForm.
Somewhere else in struts-config.xml, you will find a declaration like
- This form-bean declaration associates a logical name
CustomerForm with the actual class mybank.example.CustomerForm.
- The RequestProcessor instantiates the CustomerForm
and puts it in
appropriate scope – either session or request.
- Next, RequestProcessor iterates through the HTTP
request parameters and populates the CustomerForm properties of the
same name as the HTTP request parameters using Java
- Next, the RequestProcessor checks for the validate
attribute in the ActionMapping. If the validate is set to true, the
RequestProcessor invokes the validate() method on the CustomerForm
ActionForward execute(ActionMapping mapping,ActionForm form,
HttpServletRequest request, HttpServletResponse response) throws
- The RequestProcessor instantiates the Action class
specified in the ActionMapping
and invokes the execute() method on the CustomerAction instance.
- The signature of the execute method is as
ActionForward forward = mapping.findForward(“success”);
- The execute() method returns the next view shown to
- ActionForward is the class that encapsulates the next
- Struts encourages you not to hardcode the JSP names
for the next view.
Configuration File – struts-config.xml
- The Struts configuration file adheres to the
- The five important sections are:
- Form bean definition section
- Global forward definition section
- Action mapping definition section
- Controller configuration section
- Application Resources definition section
Form bean Definitions
Global Forward Definitions
<forward name="logon" path="/logon.jsp"/>
Message Resource Definition