Sie sind auf Seite 1von 17

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 Struts is an
open-source framework for developing Java
EE web applications. It uses and extends the
Java Servlet 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 ActionServlet and
Action classes as part of the MVC
Controller, the ActionForm class to
facilitate moving data to and from the
View, and the ActionForward class 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
And then

A Miracle Occurs
sends
Sends request to And then
Request
Browser To Web App
ActionServlet
Which
Which
Consults
Consults

web.xml

struts-config.xml

And the App Server sends a response back to the browser


Well, Not Exactly a Miracle
When the Struts ActionServlet gets a request, there are
several things that could happen. It depends on what’s in
struts-config.xml.

When ActionServlet gets 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 name attribute, ActionServlet
ActionServlet populates the form-bean
identifed by the name (circled in blue).
3. If the mapping includes a type attribute
(circled in red), ActionServlet calls the
execute() method of the Action (passing in
the request, response, form-bean from item
1, and a mapping object).
4. ActionServlet will then forward the request
to the ActionForward returned by the
execute method called in item 3.
5. If the mapping has a forward attribute,
ActionServlet simply forwards the request to
the url specified. You can’t specifiy type and
forward for the same action.
struts-
struts-config.xml
config.xml
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
DynaActionForm later). 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 each url any 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).

Das könnte Ihnen auch gefallen