Introduction to Struts

antlertextureSoftware and s/w Development

Jul 14, 2012 (6 years and 11 days ago)


Introduction to Struts
Buzzword Compliance: A quick review
of the technologies you should be
familiar with in order to understand
Struts more easily.
What Struts is.
Struts has four components
How to handle a request within the
Struts Framework
Buzzword Compliance
A structure that you can build upon
When you start a new application, a
framework allows you to not futz with
the under-workings every application
needs. It gives you a head start.
Buzzword Compliance
Java Interface
Put code in doGet and/or doPost
Usually prepare things for a Jsp to use
Impose a loose framework on your web
Buzzword Compliance
JSP (Java Server Page)
Produce HTML with Dynamic Content
Easier for Art Goobers to modify than
Get tranformed into a servlet by the App
Buzzword Compliance
Tag Library
How J2EE lets you define your own
tags you can use in JSPs.
Composed of a TLD (an xml file that
maps tags to Java classes and
methods) and a JAR (which contains
the code that runs.
JSTL is an example of a tag library.
Buzzword Compliance
XML eXtensible Markup Language
A Language for formatting data in text
Uses tags to organize data.
The web.xml deployment descriptor is
an example of an xml file that is used to
store configuration information.
Buzzword Compliance
A famous design pattern
Breaks an application into three parts
Model = Domain/Business Logic
View = Presentation/the pieces the user
sees and interacts with
Controller = The part(s) that know how
to use the model to get something done
What Struts is
According to the Struts main page: Apache
Struts is a free open-source framework for
creating Java web applications.
According to Wikipedia: Apache Strutsis an
framework for developing
web applications
. It uses and extends the
to encourage developers to
adopt a
Struts is
a Framework
It builds upon the framework provided
by J2EE and gives you a starting point
to build a web application.
It allows you to worry less about the
mundane mechanics (how requests get
handled, how do requests get routed)
It makes it a little more difficult to
deviate from the MVC pattern
Struts has 4 Main Components
The Struts Framework: Java classes
that provide the ActionServletand
Actionclasses as part of the MVC
Controller, the ActionFormclass to
facilitate moving data to and from the
View, and the ActionForwardclass to
aid the controller in forwarding
Struts has 4 Main Components
JSP Tag Libraries: Tags expand on HTML
forms and fields, help you work with beans
and provide other useful features.
Tiles Plugin: Allows you to create HTML in
re-usable pieces (tiles) that can be put
together to make a whole page
Validator Plugin: Allows you specify
validation information in an xml file and then
perform those validations in the browser
and/or the on the server.
Overall Flow: The Request Gets Passed
Around like a Bill at a Legal Seminar
Web App
And the App Server sends a response back to the browser
And then
request to
And then
A Miracle Occurs
Well, Not Exactly
a Miracle
When the StrutsActionServlet gets a request, there are
several things that could happen. It depends on what’s in
When ActionServletgets a request, it will
1.Find the action-mapping from struts-config
that matches the filename from the url.
2.If the mapping includes a nameattribute,
ActionServletpopulates the form-bean
identifedby the name (circled in blue).
3.If the mapping includes a typeattribute
(circled in red), ActionServletcalls the
execute() method of the Action(passing in
the request, response, form-beanfrom item
1, and a mapping object).
4.ActionServletwill then forward the request
to the ActionForwardreturned by the
executemethod called in item 3.
5.If the mapping has a forwardattribute,
ActionServletsimply forwards the request to
the url specified. You can’t specifiytypeand
forwardfor the same action.
What you Have to Code in Order to
Handle a Request with Struts
These are in no particular order, and you won’t
always have to do them all.
Create a class that extends Action. This class
is a controller that uses the classes in your
model. It will typically put things the request
or session for the JSP to use.
Either create a class that extends ActionForm
or define a DynaActionForm (more on
DynaActionFormlater). ActionForms are
beans whose sole purpose is to get data into
or out of the view.
Things You’ll have to Configure
in struts-config.xml
Add an <action> entry within the
<action-mappings> tag. This will map a
url(like to your Action
class (like CheckoutAction)
Add a <form-bean> entry within the
<form-beans> tag. This let’s Struts
know your ActionForm’s classname.
More Things You’ll have to
Configure in struts-config.xml
For any of your Action classes that will use an
ActionForm, make sure the name attribute of
the <action> tag matches the name from the
<form-bean> tag.
Create a <forward> tag for eachurlany of
your Actions will want to forward a request to.
The <forward> tag can either go under the
<action> tag (only that action can use it) or
under the <global-forwards> tag (any action
can use it).