Professional Documents
Culture Documents
STRUTS
PRESENTED BY
A.ANANDHI
TOPICS COVERED
Evolution of STRUTS
Model-View-Controller Architecture
Basics of Struts Framework
Sample Application Demo
Evolution of Struts
1
Request
JSP
BROWSER
(View)
Response
4 2 Data
JavaBeans 3
(Model)
1 Servlet
Request (Controller)
BROWSER
3 2
instantiate
Data
6 JSP
Response JavaBeans
(View) 4 5
(Model)
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.
Struts-config.xml
Struts Tag Libraries
View
Struts controller Servlet (HTML/JSP)
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/