Escolar Documentos
Profissional Documentos
Cultura Documentos
uk Introduction to Struts
PlanetJava
www.planetjava.co.uk
jjh@midmarsh.co.uk
Abstract
Struts is a complex piece of software in its own right. This means that time and effort
must be invested in learning Struts before it can be of benefit. This tutorial explains
what Struts is and presents some advantages and drawbacks of Struts so that you can
determine whether it is of interest to you. It then takes you step by step through the
installation of Struts and the development of two simple Struts applications. It also
explicitly describes the flow-of-control within Struts and these applications in particular
to aid your understanding.
1 Introduction
Java provides a number of technologies for building web-based applications. These include
Servlets, JSP (Java Server Pages), JSF (Java Server Faces) and JSTL (JSP Standard Tag Library).
However, none of these technologies provides a solution for how to structure web applications
(actually JSF is moving in that direction but we will ignore this for now). To overcome this
limitation many people have developed frameworks that allow standard architectures to be
employed to help structure these applications. In my own case, I was involved in developing a
Model-View-Controller (MVC) based architecture back in 2000. Building your own architecture is
not difficult and in fact Struts is to some extent just such an architecture. However, the disadvantage
of building your own is that it is not proven, you have to design, test and debug it and others are not
familiar with it. Struts on the other hand is very widely adopted, is open source (available from the
Apache organisation) and well tested. However, for simple applications the learning curve presented
by Struts may out way any benefits obtained! So what should you do? Should you use Struts?
In this tutorial we will consider what struts is, what it offers, its drawbacks and how it works. We
will also look at how to install Struts on Tomcat and how to write some simple Struts based web
applications.
1/25
www.planetjava.co.uk Introduction to Struts
2 What is Struts
Let us start by considering what Jakarta Struts is. The Struts framework can be viewed form a
number of different perspectives, these include:
1. An open source, reusable framework, available from the Apache Software Foundation. See
http://struts.apache.org
2. An implementation of the MVC design pattern.
3. A collection of utilities, that handle many of the most common tasks in Java web application
development.
4. A set of JSP custom tag libraries that simplify many common JSP operations.
Why can it be viewed form so many different angles? Essentially, because it has evolved since 2000
when its original creator Craig R. McClanahan donated it to the Apache Software Foundation. At
that time it was essentially an MVC based framework for building web applications, however as it
has been used, more and more useful features have been added to it to make web application
developer easier (at least from within the Struts framework). Thus it now possesses a basic
architecture and workflow that standardise the construction of Java web applications, a set of
utilities that provide for common operations and JSP tags that simplify the generation of the web
pages sent back to the user.
Let us take the major feature of Struts and consider it in slightly more detail – “Struts is an
implementation of the MVC design pattern”. What does this mean? The MVC design pattern (or the
Model-View-Controller design pattern to give it its full name) relates to the separation of the
display, from the business logic, from the handling of user input and is not a new idea. Indeed I first
came across the MVC architecture (as it was then called) back in 1988 [Krasner and Pope 1988]
while I was working with an object-oriented language called Smalltalk. The basic idea was to
provide a structure for building GUI based applications – and the idea still holds for Java-based web
applications (albeit with a very different implementation).
The intention of the MVC architecture is the separation of the user display, from the control of user
input, from the underlying information model as illustrated in following figure. There are a number
of reasons why this is useful:
Display/ Control of
View User Input
Information
Model
2/25
www.planetjava.co.uk Introduction to Struts
This means that different interfaces can be used with the same application, without the application
knowing about it. It also means that any part of the system can be changed without affecting the
operation of the other. For example, the way that the graphical interface (the look) displays the
information could be changed without modifying the actual application or how input is handled (the
feel). Indeed the application need not know what type of interface is currently connected to it at a ll.
Inturn the information model can be modified without the control or view aspects altering.
The Model-View-Controller aspects of the Struts framework are illustrated in the figure below.
Application
2. executes Controller 3. calls behaviour
(Action)
1. Event
(http request) Struts
Front Controller Model
Servlet (JavaBeans)
6. Update
(http response)
3/25
www.planetjava.co.uk Introduction to Struts
HTML page is then returned to the requesting browser. In this case the view has been implemented
by a JSP file. There is no flow logic, no business logic, and no model information in the view -- just
tags. Note that this means that you must write a JSP containing Struts tags to provide the GUI.
4/25
www.planetjava.co.uk Introduction to Struts
5/25
www.planetjava.co.uk Introduction to Struts
6/25
www.planetjava.co.uk Introduction to Struts
7 Struts Applications
Before we can start to think about creating Struts based Java web applications, we need to consider
how Java-based web applications are constructed. This is because, at the end of the day, Struts is a
Java-based web application. The Servlet 2.2 specification introduced the notion of a web
application, which has been further refined in the 2.3 and 2.4 specifications. Each web application
has:
7/25
www.planetjava.co.uk Introduction to Struts
shop
webshop
WelcomeServlet.class
• shop the root directory for the web application. The name of this directory is defined in the
web application context in Tomcat.
• <html files> All html files located at the web application root are visible to the external
web.
• WEB-INF the contents of this directory is hidden from the web server and contains the class
files defining the Servlets and any supporting classes. Any JAR files used by the web
application should be placed within the lib directory. If the class files are not Jar’ed up then
they must be placed within the classes sub directory. For example in Error! Reference
source not found., the webshop package containing the WelcomeServlet class.
• web.xml this file is an xml file that defines mappings between URLs and Servlets and JSPs.
It can also specify filters to apply, security domains, session timeouts etc.
Struts modifies this structure by adding its own files into the architecture. Primarily it adds the struts
jar files to the lib directory (where they will be picked up by the Tomcat environment) as illustrated
in Figure 5. Struts also requires a number of Tag Library definitions files and XML files to be
placed in the WEB-INF directory of a web application. These files include the struts-config.xml
file used to configuration the navigation, validation etc. that will happen within a web application.
These files are illustrated in Figure 6.
8/25
www.planetjava.co.uk Introduction to Struts
9/25
www.planetjava.co.uk Introduction to Struts
blank directory into this. It will make things easier later on.
2. executes Application
3. calls behaviour
Action
7. Update
(http response)
1. When data is sent to the Struts framework from a browser the action requested is
<something>.do.
2. When the request for <something>.do is received by the Struts Servlet, this Servlet looks up
the set of mappings provided by the struts-config.xml file. The mappings in this file link
requests to Actions. The Struts Servlet then calls the execute method on the appropriate
action object.
3. When this execute method terminates it returns a result indicating what should happen next
(for example it might return “success” or “failure”).
4. The mappings in the struts-config.xml file indicate which View should be displayed next
based on these return results.
5. The Servlet then forwards the request on that view so that it can generate the resulting web
page.
Note that we have simplified the Struts Servlet concept here by implying that it is a single Servlet, it
is not – the Struts framework is quiet complex, but it makes life easier to view what Struts is doing
as a single front controller type Servlet. Also note that there is nothing magic about Struts – it is
10/25
www.planetjava.co.uk Introduction to Struts
doing exactly what you tell it to do. You write the configuration file, you right the Action objects,
the JavaBeans and the Views. It is just that Struts does the coordination or “flow-of-control”
elements for you.
1. Plan the navigation and code in XML within the struts-config.xml file.
2. Create form that invokes <something>.do
3. Define a form bean (if you have data to submit)
4. Create an Action object to handle the request.
5. Define a results bean (if you need to pass data to the view form the action)
6. Create a JSP view to return to the user
11/25
www.planetjava.co.uk Introduction to Struts
12/25
www.planetjava.co.uk Introduction to Struts
Note that in the above example we have placed our JSP view within a directory called jsps within
the WEB-INF directory. It will thus only be available to the Struts web application.
10.2 Step 2: Create the form to request the action
We now need to create a HTML form that will request the Struts framework to execute the
WelcomeAction (we have yet to define). We could use the Struts form tag but as the standard
HTML form is rather more widely known (and in a bid to minimize the introduction of any
unnecessary complexity for the time being) we shall adopt it. Thus we will define a very simple
HTML form whose action attribute specifies welcome.do. Note that the form specifies the action
with a .do extension but that the struts-config.xml action mapping does not.
The HTML form we have defined will be in a file called index.html (just to make things as simple
as possible). Note that we have used the POST method but the GET method could have been used
with the form. The HTML page containing the form is presented below:
13/25
www.planetjava.co.uk Introduction to Struts
The execute method must process the specified HTTP request and can obtain the data passed to it
via the ActionForm bean (if one is available). Note it also has access to the original request and
response objects. This is illustrate din the following example:
14/25
www.planetjava.co.uk Introduction to Struts
In the above example, the execute method merely prints out a welcoming message (to the Tomcat
standard output) and returns a value back to the struts framework that indicates successful
processing of the request.
15/25
www.planetjava.co.uk Introduction to Struts
../webapps/HelloStrutsWorld/index.html
../webapps/HelloStrutsWorld/WEB-INF/struts-config.xml
../webapps/HelloStrutsWorld/WEB-IN/classes/uk/co/experis/bookstore/WelcomeAction.class
../webapps/HelloStrutsWorld/WEB-INF/jsps/welcome.jsp
http://localhost:8080/HelloStrutsWorld/index.html
16/25
www.planetjava.co.uk Introduction to Struts
If we now click on the “Enter” button, which will send a request to Struts for the welcome.do action
to be performed, then we will see the next web page displayed:
Thus the request has been sent to Struts, Struts mapped this request to the WelcomeAction (have a
look at the Tomcat output window for the WelcomeAction.execute() printout), which returned a
“success” result which caused Struts to request the welcome.jsp page to be displayed. This flow-of-
control is illustrated below for this web application:
17/25
www.planetjava.co.uk Introduction to Struts
Welcome
Action
2. executes
3. returns "success"
1. submit
Struts 2. maps request to action
(requests welcome.do)
Front Controller struts-config.xml
Servlet 4. maps result to view
index.html
5. forwards welcome.jsp
7. Update
(http response)
18/25
www.planetjava.co.uk Introduction to Struts
Form beans are defined within the <form-beans> element. This element contains one or more
<form-bean> elements. This element defines a form bean. Thus:
In Figure 16 a form bean called book is defined which will be an instance of the class
uk.co.Experis.bookstore.Book. Notice that this merely defines a form bean that can be used with
data input from a form. It does not specify which form will provide that data nor which actions will
use it. This is done within the <action> elements.
19/25
www.planetjava.co.uk Introduction to Struts
The other difference in this action compared to the previous WelcomeAction is that it has two
outcomes. One for “success” and one for “failure”. There are therefore to <forward> elements, on
for each result. The “success” outcome is mapped to the bookbought.jsp and the “failure” outcome
is mapped to the bookfailure.jsp.
20/25
www.planetjava.co.uk Introduction to Struts
methods for all properties. The actual Book Form Bean class I presented below.
21/25
www.planetjava.co.uk Introduction to Struts
22/25
www.planetjava.co.uk Introduction to Struts
The more complex bookbought.jsp is displayed if the execute method of the Action returns
“success”. This JSP accesses the Receipt result bean and displays information it holds. It does this
using the bean:write tag provided as part of the Struts tag libraries (not using standard JSP
useBean and getProperty tags you could achieve the same effect as bean:write however the Struts
tag is more elegant). This is the more common approach when using Struts. It does require that the
Struts bean tag library is included in the JSP (see line 4 in Figure 21). To use the bean:write tag
itself you specify the name of the bean to access and the name of the property on that bean (as
illustrate din line 18 of Figure 21). Note that the name of the bean is the name under which the
results bean was placed in the request object by the Action class.
23/25
www.planetjava.co.uk Introduction to Struts
24/25
www.planetjava.co.uk Introduction to Struts
13 References
[Alur, Crupi and Malks 2001] D. Alur, J. Crupi and D. Malks, Core J2EE Patterns: Best Practices
and Design Strategies, Prentice Hall, 0-13-064884-1, 2001.
[Cavaness 2004] C. Cavaness, Programming Jakarta Struts (2nd Ed.), O’Reilly, 0-596-00651-9,
2004.
[Krasner and Pope 1988] G. E. Krasner and S. T. Pope, A Cookbook for Using the Model-View
Controller User Interface Paradigm in Smalltalk-80, JOOP 1(3), pp. 26-49, 1988.
25/25