Introduction to Struts

antlertextureΛογισμικό & κατασκευή λογ/κού

14 Ιουλ 2012 (πριν από 5 χρόνια και 4 μήνες)

408 εμφανίσεις

Introduction to Struts
Contents
￿
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
Framework
￿
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
Servlet
￿
Java Interface
￿
Put code in doGet and/or doPost
￿
Usually prepare things for a Jsp to use
￿
Impose a loose framework on your web
app.
Buzzword Compliance
JSP (Java Server Page)
￿
Produce HTML with Dynamic Content
￿
Easier for Art Goobers to modify than
servlets
￿
Get tranformed into a servlet by the App
Server
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
files.
￿
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
Model-View-Controller
￿
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
open-source
framework for developing
Java
EE
web applications
. It uses and extends the
JavaServlet
API
to encourage developers to
adopt a
model-view-controller
(MVC)
architecture.
Struts is
(mainly)
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
1.
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
requests.
Struts has 4 Main Components
2.
JSP Tag Libraries: Tags expand on HTML
forms and fields, help you work with beans
and provide other useful features.
3.
Tiles Plugin: Allows you to create HTML in
re-usable pieces (tiles) that can be put
together to make a whole page
4.
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.xml
Web App
Browser
Which
Consults
And the App Server sends a response back to the browser
ActionServlet
And then
sends
request to
Sends
Request
To
Which
Consults
struts-config.xml
And then
A Miracle Occurs
Well, Not Exactly
a Miracle
ActionServlet
When the StrutsActionServlet gets a request, there are
several things that could happen. It depends on what’s in
struts-config.xml
.
strutsstrutsstrutsstruts-
---config
configconfig
config.xml
.xml.xml.xml
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 checkout.do) 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).