You are on page 1of 19

Jakarta Struts

Web Application Framework

An overview of Jakarta Struts with information


to assist in evaluating Struts for use within a
development group.

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

“A structure for supporting or enclosing something else,


especially a skeletal support used as the basis for
something being constructed.”

Struts provides the ‘framework’, or shell, of an


application to save time for developers.

By Mohseen Mansoor
mohseen.mansoor@hotmail.com
Struts is a Web Application 'Framework'?
Framework Definition 2

“A set of assumptions, concepts, values, and practices


that constitutes a way of viewing reality.”

Struts provides a ‘way of looking a things’ that helps


developers break complex applications into simple,
consistent sets of components.

By Mohseen Mansoor
mohseen.mansoor@hotmail.com
... And Frameworks are important because?

• Saves developers time


• Many “eyes on the code” increases stability
• Encodes best practices
• Highly platform independent
• Gives developers ‘a place to start’
• Ensures all developers develop code using the
same approach.

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).

Model Components - Access Data and Systems


View Components - What the users see
Controller Components - Direct the Action

By Mohseen Mansoor
mohseen.mansoor@hotmail.com
Struts Model Components

Provide a 'model' of the business logic or data.

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

Presentation layer for interacting with users.

Characteristics:

- Display data from Model Components


- Written using JavaServer Pages (JSP)
- May accept and validate some input
- Struts Tags simplify building Views

By Mohseen Mansoor
mohseen.mansoor@hotmail.com
Struts Controller Components

Control Program Flow and Manage Exceptions.

Characteristics:

- Accept Input from Users


- Determine which Models to Update
- Determine which Views to Display
- Struts Tags simplify building Views

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:

• You need applications to run in a Web Browser.


• Your developers have Java expertise.
• You are currently using JavaServer Pages (JSP).
• You are using the J2EE Platform (Tomcat, Weblogic, etc)
• You are considering building an application ‘Framework’.

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

You might also like