Welcome to Scribd, the world's digital library. Read, publish, and share books and documents. See more
Standard view
Full view
of .
Look up keyword
Like this
0 of .
Results for:
No results containing your search query
P. 1
Struts Notes

Struts Notes

Ratings: (0)|Views: 20 |Likes:
Published by Ankit Jain

More info:

Published by: Ankit Jain on Aug 27, 2012
Copyright:Attribution Non-commercial


Read on Scribd mobile: iPhone, iPad and Android.
download as DOC, PDF, TXT or read online from Scribd
See more
See less





Overview of Struts
What is Struts?
Struts is open source implementation of Java MVC (Model-View-Controller) pattern for the development of web based applications on JEE plateform
The core of the Struts framework is a flexible control layer based on standard technologies like Java Servlets,JavaBeans, ResourceBundles, and XML).
Struts encourage application architectures based on the Model 2 approach, a variation of the classic Model-View-Controller (MVC) design paradigm.
Struts provides its own Controller component and integrates with other technologies to provide the Model and theView
Struts can interact with standard data access technologies, like JDBC and EJB, as well as most any third-party packages, like 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 presentation systems.
Model 1 design PatternProblems with Model1 Architecture
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 developers
In large applications, JSPs are developed and maintained by page authors. The intermingled scriptlets and markupresults 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 inthe current page. Decentralized navigation control can cause headaches.
MVC Design Pattern
MVC suggests that an application should have three parts
Model : -
Represents system state and business logic beans. E.g java beans and enterprise beans
View :-
View portion represents the user interface and for a J2EE web application is most oftenConstructed using Java Server Pages (JSP) technology
Controller :-
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
Model 2 Architecture - MVC
The Controller Servlet handles the user’s request. (This means the hyperlink in the JSP should point to thecontroller servlet).
The Controller Servlet then instantiates appropriate JavaBeans based on the request parameters (and optionallyalso based on session attributes).
The Controller Servlet then by itself or through a controller helper communicates with the middle tier or directlyto 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 or application.
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 display data.
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 toeach 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 coordinationamong team members.
The Struts Framework 
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 (determines navigation)
Controller uses mappings to turn HTTP requests into application actions
Mapping must specify
A request path
Object type to act upon the request
Struts Environment
In Struts, there is only one controller servlet for the entire web application.This controller servlet is called
The servlet intercepts every client request and populates an
from the HTTP request parameters.
is a normal JavaBeans class. It has several attributes corresponding to the HTTP request parametersand getter, setter methods for those attributes. You have to create your own
for every HTTP requesthandled through the Struts framework by extending the ActionForm class.
They are essentially View Data
Transfer Object.
View Data Transfer Object is an object that holds the data from html page and transfers it around in the web tier framework and application classes.
How the Struts Framework Work?
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
The Handler is called
in the Struts terminology
The Action class is abstract and defines a single method called
().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.
Struts Framework Components
ActionForwardAll residing in org.apache.struts.action package.ActionServlet
The central component of the Struts Controller is the ActionServlet.
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
entry for configuring the
Struts Config file
is as follows<servlet><servlet-name>action</servlet-name><servlet-
ActionServlet intercepts HTTP requests based on the URL pattern and handles them appropriately. The URL pattern can be either path or suffix.

You're Reading a Free Preview

/*********** DO NOT ALTER ANYTHING BELOW THIS LINE ! ************/ var s_code=s.t();if(s_code)document.write(s_code)//-->