Professional Documents
Culture Documents
By Mohseen Mansoor
mohseen.mansoor@hotmail.com
What is Struts and Why Should I Care?
• Web Application Framework based on
J2EE and Java
• Provides components for building Web
applications that speed development
• Many users help debug and improve it.
• Open Source: Access to source and no cost.
• No licenses to keep track of and renew.
• Dramatic growth in usage.
By Mohseen Mansoor
mohseen.mansoor@hotmail.com
Struts is a Web Application 'Framework'?
Framework Definition 1
By Mohseen Mansoor
mohseen.mansoor@hotmail.com
Struts is a Web Application 'Framework'?
Framework Definition 2
By Mohseen Mansoor
mohseen.mansoor@hotmail.com
... And Frameworks are important because?
By Mohseen Mansoor
mohseen.mansoor@hotmail.com
How Does Struts Work?
Struts uses Model-View-Controller (MVC)
design pattern. MVC is well established and
time-proven (in use since 1970’s).
By Mohseen Mansoor
mohseen.mansoor@hotmail.com
Struts Model Components
Characteristics:
- Interface to Databases
- Encode Business Logic and Calculations
- Interface to Remote Systems
- Save Data Entered by Users
By Mohseen Mansoor
mohseen.mansoor@hotmail.com
Struts View Components
Characteristics:
By Mohseen Mansoor
mohseen.mansoor@hotmail.com
Struts Controller Components
Characteristics:
By Mohseen Mansoor
mohseen.mansoor@hotmail.com
Our Example – Simple Login
• ActionServlet controls the navigational flow
• Action class used to access business classes
• When ActionServlet receives request from
container, it uses the URI to determine
which Action needs to handle the request
• An Action can validate input and access
business layer to retrieve data from database
By Mohseen Mansoor
mohseen.mansoor@hotmail.com
Login (cont)
• Action does not pull values from web page
but bundles this input into a JavaBean
• Input beans are subclasses of the Struts
ActionForm class.
• ActionServlet knows which ActionForm to
use by path
By Mohseen Mansoor
mohseen.mansoor@hotmail.com
Login(cont)
• Action does not render response itself but
forwards to (ex) JSP page. Uses
ActionForward class to store path to here
• After finishing business logic, Action
selects and returns an ActionForward to the
servlet.
• Servlet uses info from here to call the page
and complete the response
By Mohseen Mansoor
mohseen.mansoor@hotmail.com
Login(cont)
• ActionMapping object – bundles details of
forward requests (tells servlet which Action,
ActionForm, and ActionForward to use)
• All are declared in the struts-config.xml file
• ActionServlet reads this file at startup time
and creates a database of configuration
objects.
By Mohseen Mansoor
mohseen.mansoor@hotmail.com
Login (cont)
Success.html Failure.html
JSP JSP
response Form
submit
Initial Page ActionServlet RegisterForm.class
(JSP/HTML)
Register.jsp
Action
struts-config.xml RegisterAction.class
By Mohseen Mansoor
mohseen.mansoor@hotmail.com
Our Classes
• RegisterForm – our ActionForm JavaBean to
capture input fields sent by the request.
ActionServlet matches property on request to
properties of this ActionForm and ActionServlet
calls setter method
• RegisterAction - Our RegisterForm is then passed
to this Action which validates input, accesses
business info, and determines which
ActionForward to return to the servlet
By Mohseen Mansoor
mohseen.mansoor@hotmail.com
Our Classes(cont)
• UserDirectory – helper class that records
usernames and passwords into a standard
properties file
• NOTE: the perform method returns the
ActionForward after passing it the
ActionMapping
• What are “failure” and “success”?
By Mohseen Mansoor
mohseen.mansoor@hotmail.com
Our classes (cont)
• Look at struts-config.xml
• This is the deployment descriptor
• NOTE it defines the form-bean and the
action-mappings
• NOTE: path is register – usually ends in
/do/ or .do – then the container knows to
forward the request to our ActionServlet –
Struts trims the extension automatically
By Mohseen Mansoor
mohseen.mansoor@hotmail.com
Our classes(cont)
• Success.html and failure.html normal pages
• Start up page – register.jsp
– Uses a JSP custom tag library for forms (struts-
form.tld) – cool!!! Looks real different but all
built into Struts – under the covers are the
TagHandler classes. Also has one for html,
logic, and beans.
By Mohseen Mansoor
mohseen.mansoor@hotmail.com
Is Struts for You?
Struts may be for you if some of these are true:
The more of the above that are true, the greater the likelihood
that Struts makes sense for your development group.
By Mohseen Mansoor
mohseen.mansoor@hotmail.com