You are on page 1of 35

INTRODUCTION TO

STRUTS

PRESENTED BY
A.ANANDHI
TOPICS COVERED

 Evolution of STRUTS
 Model-View-Controller Architecture
 Basics of Struts Framework
 Sample Application Demo
Evolution of Struts

Client Side Programming:


1.Started with CGI scripts for dynamic
generation of HTML
-for every new request, the server had to
create a new process .
-took longer response time
-heavy on memory requirements
2.Then came Servlets
-multithreading processing
-efficient performance
However,
-required too many out.println statements
-after every code change,it needs to be
re-compiled and also the server has to be
re-started.
3.Then came Java Server Pages (JSPs)
- JSPs are extension of the servlet
technology
-It provides the facility of embedding java
code within a HTML page
-have all the advantages of servlets
-any modification in the JSP page can be
easily reflected by simply refreshing the
page.
4. Use of JSPs and Servlets
-JSPs were used for presentation
-Servlets were used for Business logic
-MVC architecture was adopted in order
to separate the business logic & data of
the application from the presentation of
the data to the user.
5.Struts was then developed as an open source
framework for building web applications using
the MVC design pattern.
-Struts provides the basic infrastructure needed
for building any web application
-provides services common to most web
applications
-developers can concentrate on the business
logic.
Hence, Struts helps developers deploy
products that are complicated,reliable,scaleable
and easier to maintain over time.
MODEL VIEW CONTROLLER
Architecture
 MVC is well suited for interactive web
applications; where a web user interacts with
a web-site, with multiple iterations of screen
page displays and multiple requests for
displaying data.
 It promotes complete separation between

business logic and presentation by dividing


the application into three layers and decoupling
their respective responsibilities
MODEL:
 represents

a)Internal State of the system


(business data)
b)Actions that can be taken to change the
state (application business logic)
 It does not have any idea about how the
data will be displayed by the application.
VIEW:
 Represents the presentation of the
application. It gets the data from the model
for its content and displays it.
 The view is not dependent on the data or
application logic.
 The model notifies the view when there is
change in the state of the model
 The view also forwards user input to a
controller.
CONTROLLER:
Defines the application behavior
All user requests to the application go thru
the controller
It intercepts requests from the view,
interprets the user inputs and maps them to
a model for appropriate action(business
operation to be performed).
Depending upon the user interactions and
the outcome of the model operations,it
selects the next view to be displayed to the
user.
Web Tier Architectures
Model 1 :

1
Request
JSP
BROWSER

(View)
Response
4 2 Data

JavaBeans 3
(Model)

Application Server Data Sources


 Web browser directly accesses Web-tier
JSP pages.
 The JSP pages access Web-tier
JavaBeans that represent application
model
 The next view to be displayed is
determined either by hyperlinks selected
in the source document or by request
parameters.
Disadvantages:
A model 1 application control is decentralized as the
current page being displayed determines the next
page to display
Each JSP page or servlet processes its own inputs
(parameters from GET or POST)
Choosing the next page to be displayed may be
incorporated in scripted code.
The coder of the JSP file must be both a page
designer and a java developer
As the pages becomes larger,there is more
possibility of JavaScript getting confused with Java
code.eg. Using client side JavaScript to validate the
email field.
Difficultto understand the logical flow of the
application
In addition to being ugly to look at,HTML
tags,Java code and JavaScript code all in one
page makes it difficult to debug problems
Although HTML and JavaScript can be added to
pages using a JSP mechanism called
“include”.However,developers have a tendency to
create huge JavaScript files and these libraries are
included into the JSP file.
The result is that a much larger than necessary
HTML page is returned to the client.
MODEL 2:

1 Servlet
Request (Controller)
BROWSER

3 2
instantiate
Data
6 JSP
Response JavaBeans
(View) 4 5
(Model)

Application Server Data Source


 Model 2 architecture introduces a
controller servlet between the browser and
the JSP pages or servlet content being
delivered.
 The controller centralizes the logic for
dispatching requests to the next view based
on the the request URL,input parameters
and application state.
 With this model,there is clear separation
of business logic,presentation output and
request processing(refered as MVC pattern).
 Since the controller provides a single point
of entry,it can be used for
authorization,security and logging operations.It
often encapsulates incoming data into a form
usable by the back-end MVC model(using
view beans).
 Code that would normally need to be put in
every JSP page can be put in the controller
servlet, since it processes all requests
 Model 2 applications are easier to maintain
and extend as the views do not refer to each
other directly.
MVC Model
MODEL State Change
State query •Encapsulates application state
•Responds to state queries
•Exposes application functionality
•Notifies view of the changes

Change
Notification View
Selection
CONTROLLER
VIEW
•Defines application behavior
•Renders the models
•Maps user action to model updates
•Requests updates from models
•Selects view for response
•Sends user gestures to controller
•One for each business functionality

User Gestures

Method Invocation
Events
Struts as MVC Implementation
VIEW Components:
 HTML/JSP
 JavaScript and Style sheets
 Struts and custom tag libraries
 Resource bundles
 ActionForm classes
Struts as MVC Implementation
(contd..)
CONTROLLER Components:
 ActionServlet
(org.apache.struts.action.ActionServlet)
 RequestProcessor
 Struts-config.xml file
 ActionMapping
 Action classes
Struts as MVC Implementation
(contd..)
MODEL components:
 No built-in support by Struts Framework
 Needs to be implemented by the
developer
 May consist of
- JavaBeans
- EJB
- CORBA etc
STRUTS- The Basics
Struts is a framework that implements MVC.
 Consists of a set of classes or components that
provide generic behavior that can be utilized
across building different types of applications.

 Addresses problems in View & Controller parts


of MVC

 Originally created by Craig R. McClanahan


And later added to Apache Software Foundation.
1.Problems addressed in View:
Mixing code with presentation that results in
-Redundancy
-Readability
-difficult maintenance

2.Problems addressed in Controller:


-hardcoding resource details in Servlet

With these problems, the developer needs to


concentrate on business objects, presentation and
also request processing.
 All these problems are addressed by
emphasizing on building components and
reusability of components.
 Provides centralized point of control for
web applications.
 Because of single point of entry,this
framework provides facilities like
- centralized logging
- authentication
- authorization
- internationalization
Struts Flow Diagram
Request Response
web.xml

Struts-config.xml
Struts Tag Libraries
View
Struts controller Servlet (HTML/JSP)

Action Action Resource


class class Properties
File

MODEL

Data
How Does It All Work
1.On Server Start up:
- The server searches for configured web
applications
- If configured properly,it recognizes the existence of
the root directory of the web application and
searches for “WEB-INF” directory under the root.
- In “WEB-INF”, there must already be created and
configured an xml file called “web.xml”.This file
contains the default behavior and configurations for
our application, that the server reads and uses.
-The “web.xml” is configured to load upon startup,
the struts “ActionServlet” class.
2. On Controller Servlet loading
- As the ActionServlet is loading,it searches for the
“struts-config.xml” file.
- ”struts-config.xml” file contains the configuration
information for the behavior of the Servlet itself.It
contains the associations between the Struts
Components.
- As the “ActionServlet” reads “struts-config.xml”,it
begins to create and register struts based classes
which we have designed and mentioned in the
file.It places them in memory associated with a
lookup name.
The Servlet is now ready to react to requests
3.After Servlet Controller has been
loaded:
- User sends out an URI request from the
browser e.g.:
http://www.MySite.com/index.do
- The ActionServlet would ideally be
configured to respond to any URI request
that matches a particular String (for e.g.: any
URI that ends with “.do”).
- The ActionServlet will strip off the “.do” and
will try to look up an Action Class by
matching the remaining un-stripped string
e.g.:/index
- The ActionServlet now uses this looked up
class(of type Action) to process request.
- A different request with a different URL
ending could have called upon a different
Action class to perform a different function.
- The controller will create only a single
instance of each Action Class for the
application.Hence,it must be ensured that all
the Action Classes operate properly in a
multithreaded environment.
- If an ActionForm instance of the appropriate scope
and type is present for this new request, then re-use it.
- Otherwise, create a new instance and store it in the
appropriate scope.
- The reset() method is called on the ActionForm
instance.
- Iterate through the request parameters and populate
their values in the ActionForm using the corresponding
Set-Method.
- After the user data is loaded into the ActionForm(view
bean),the “validate()” method is automatically called.
- If no validation problems,the controller will proceed to
call the “execute()” method of the appropriate Action
class.
- If there are validation problems,an instance of
“ActionErrors” is returned that contains the error
message keys that should be displayed.
-The actual values for the error message keys are
mentioned in the application’s MessageResource
bundle.
-The controller stores the ActionErrors collection as
a request attribute suitable for use by the
<html:errors/> tag and
Will forward control to the input form(identified by
the input property for this ActionMapping).
- The execute() method of the Action class
a)does all “What To Do” functions
b)Calls whatever model classes it needs
to call to perform the actual business
validations & operations (“How to Do”)
c)finally looks up or creates an
“ActionForward” object which it returns.
- An “ActionForward” object contains the
destination URI.
- The Controller then forwards/redirects the
resulting destination URI to the user.
Sequence Diagram
User Servlet Action JSP Service Data

http://www…

execute()
function() get()

return
return
ActionForward

forward

Html page
References

http://jakarta.apache.org/struts/doc-1.0.2/userGu
http://www.scioworks.net/devnews/articles/struts_intro_f
http://www.ibm.com/developerworks/ibm/library/j-struts
http://www.onjava.com/

You might also like