Académique Documents
Professionnel Documents
Culture Documents
William W. Provost
Student Guide
Revision 1.1.1
Jakarta Struts
Jakarta Struts
Rev. 1.1.1
Student Guide
Information in this document is subject to change without notice. Companies, names and data
used in examples herein are fictitious unless otherwise noted. No part of this document may be
reproduced or transmitted in any form or by any means, electronic or mechanical, for any
purpose, without the express written permission of Object Innovations.
Other product and company names mentioned herein are the trademarks or registered trademarks
of their respective owners.
Copyright 2003 Object Innovations, Inc. and William W. Provost All rights reserved.
Object Innovations, Inc.
290 Turnpike Rd., Suite 413,
Westborough, MA 01581-2843
877-558-7246
www.objectinnovations.com
Printed in the United States of America.
Rev. 1.1.1
ii
Jakarta Struts
Struts Architecture
Action Mappings
Form Beans
Persistent Data
Struts Tag Libraries
The JSP Standard Tag Library
Internationalization and Localization
Validating Input
Under the Hood
Best Practices
Tiles
Appendix A
Learning Resources
Rev. 1.1.1
iii
Jakarta Struts
Prerequisites
Struts is a framework for Web application
development using Java servlets and JSPs.
This course treats Struts in depth and therefore
requires students to have several skill sets coming
into the class:
HTML page authoring, at least at a basic level of fluency
Java programming, including fluent use of Java classes,
interfaces, inner classes, etc.
Java servlets programming, including interactive Web
applications, session attributes, initialization parameters,
configuration and context
JavaServer Pages (JSP) development, preferably to the
level of good familiarity with standard actions and custom
tags
Rev. 1.1.1
iv
Jakarta Struts
Labs
The module relies on hands-on experience in various
topics and techniques.
All lab code is 100% Pure Java, written to build and
run on the J2SE SDK, version 1.4.
Students will also work with a number of file types,
all written to the applicable Java or W3C standard:
HTML pages (.html)
Java Server Pages (.jsp) and fragments
SQL scripts (.sql)
XML documents (.xml)
Rev. 1.1.1
Jakarta Struts
Lab Installation
The installation files for this course have been
provided to you by your courseware vendor, training
coordinator, or instructor.
Well refer to an installation root directory, which may be a
shared network drive, or possibly the root directory of a CDROMs or floppy disk which youve been given.
Your instructor can direct you as to where to find this
material.
Rev. 1.1.1
vi
Jakarta Struts
Rev. 1.1.1
vii
Jakarta Struts
Rev. 1.1.1
viii
Jakarta Struts
ix
Jakarta Struts
Jakarta Struts
JDBC........................................................................................................................... 126
Connection, Statement, and ResultSet........................................................................ 127
Sample Code ............................................................................................................... 128
Two Paths to a Connection ......................................................................................... 129
Many Paths to a DataSource....................................................................................... 130
The Struts DataSource Manager ................................................................................. 131
The <data-source> Element ........................................................................................ 132
MySQL ....................................................................................................................... 133
MySQL Server Setup.................................................................................................. 134
MySQL Database Setup.............................................................................................. 135
Example Web Retailing ........................................................................................... 136
Multi-Tier Architecture............................................................................................... 140
Business Logic Beans ................................................................................................. 141
Persistence Code ......................................................................................................... 142
EJB.............................................................................................................................. 143
Demo A Database for LoveIsBlind ......................................................................... 144
Lab 4 ........................................................................................................................... 149
Summary..................................................................................................................... 150
Chapter 5: Struts Tag Libraries.................................................................................. 153
Building View Components........................................................................................ 155
Struts Tag Libraries .................................................................................................... 156
Deployment of Custom Tags ...................................................................................... 157
Struts Tag Library Namespaces.................................................................................. 158
Attributes for Struts Tags............................................................................................ 159
The HTML Tag Library.............................................................................................. 160
Building Forms ........................................................................................................... 161
The <html:form> Tag ................................................................................................. 162
The <html:text> Tag, et. al. ........................................................................................ 163
Forms and Form Beans ............................................................................................... 164
Scenarios for Data Transfer ........................................................................................ 165
Scope and Duration of Form Data .............................................................................. 166
Demo LoveIsBlind Forms ....................................................................................... 167
Managing Hyperlinks ................................................................................................. 174
Example Links in the Results Page.......................................................................... 175
The <html:errors> Tag................................................................................................ 176
The Logic Tag Library................................................................................................ 177
Other Struts Tag Libraries .......................................................................................... 178
Case Study Next Steps............................................................................................. 179
Lab 5 ........................................................................................................................... 180
Summary..................................................................................................................... 181
Chapter 6: The JSP Standard Tag Library ............................................................... 187
The JSP Standard Tag Library.................................................................................... 189
The JSP Expression Language.................................................................................... 190
Using JSP Expressions................................................................................................ 191
EL Syntax ................................................................................................................... 192
JSTL Namespaces....................................................................................................... 193
Rev. 1.1.1
xi
Jakarta Struts
xii
Jakarta Struts
xiii
Jakarta Struts
xiv
Jakarta Struts
Rev. 1.1.1
xv
Jakarta Struts
Chapter 7
Chapter 7
Internationalization and
Localization
Rev. 1.1
219
Jakarta Struts
Chapter 7
Rev. 1.1
220
Jakarta Struts
Chapter 7
Rev. 1.1
221
Jakarta Struts
Chapter 7
i18n in Java
The J2SE provides complete support for
internationalization, although the APIs are not the
most facile things in the world.
The framework is based on a handful of classes in the
java.util package:
Locale
TimeZone
ResourceBundle
Rev. 1.1
222
Jakarta Struts
Chapter 7
Locales
In order to produce output that will make the most
sense to a human user, an application first needs to
understand a few things about that users locale:
The language he or she reads and writes. This will influence
the language used in application messages and output.
The place from which he or she is using the application. This
will affect the format in which certain output is produced.
The time zone from which he or she is using the application.
This will affect date and time values, rather than formats.
Rev. 1.1
223
Jakarta Struts
Chapter 7
Determining Locale
The application can set its locale preference.
This is typically in response to some user input perhaps a
configuration performed once when a user creates a profile.
This may also be done purely for testing purposes.
Rev. 1.1
224
Jakarta Struts
Chapter 7
Time Zones
Java software uses the java.util.TimeZone class to
encapsulate information about a time zone.
Again, there is a common string representation as well, in
this case specified as part of the Java standard platform.
Two formats are possible: a terse one stating GMT plus or
minus some number of hours, and a verbose one in which the
names of places are written longhand and separated by
slashes, as in America/Los_Angeles.
Rev. 1.1
225
Jakarta Struts
Chapter 7
Resource Bundles
A resource bundle is a collection of named resources
dedicated to a specific locale.
An application will commonly deploy several resource
bundles, one for each locale or language supported for
example separate bundles for US English, UK English,
French and Spanish.
Each such bundle will have the same set of keys, but
different values for those keys.
The application relies on lookups into these bundles to
present a locale-specific interface from the same Java code.
226
Jakarta Struts
Chapter 7
Rev. 1.1
227
Jakarta Struts
Chapter 7
Rev. 1.1
228
Jakarta Struts
Chapter 7
Rev. 1.1
229
Jakarta Struts
Chapter 7
Rev. 1.1
230
Jakarta Struts
Chapter 7
Config\classes\application.properties:
error.serverException=<p>An error was encountered
in processing this request. The calculator memory
has been cleared; you can try again!</p>
View\Calculator.jsp:
<b style="color:red" ><html:errors /></b>
Rev. 1.1
231
Jakarta Struts
Chapter 7
232
Jakarta Struts
Chapter 7
Rev. 1.1
233
Jakarta Struts
Chapter 7
i18n in Validation
In the next chapter well take a long look at the Struts
Validator.
It is here that internationalization first imposes itself
upon many Struts developers.
Validation rules and the validator framework in general are
mostly about generically handling problems.
This means declaring messages that should be handed back to
the user when problems arise.
Message keys are the natural way to do this, and Struts
philosophy on internationalization seems to be, In for a
penny, in for a pound.
So, where in other aspects of development one might choose
to forego internationalization for early iterations, when it
comes to validation there is really no other way to go about it
(except to build ones own validator).
Rev. 1.1
234
Jakarta Struts
Chapter 7
Lab 7
An Authentication Error Message
In this lab you will enhance the existing Love is Blind logon page.
It currently does nothing to inform the user when authentication
fails it is just shown repeatedly until the user gets it right or goes
away. You will add code to Logon.jsp to present an advisory
message, and a key and value to the default resource bundle to
support the JSP.
Detailed instructions are contained in the Lab 7 write-up at the end
of the chapter.
Suggested time: 30-45 minutes.
Rev. 1.1
235
Jakarta Struts
Chapter 7
Summary
On the other hand, Struts thoroughness with, and
insistence on, internationalization can be helpful.
The use of a default resource bundle, and the
consistent use of message keys throughout the system,
makes it fairly simple to get internationalization
support involved in the very first iteration of a Struts
application.
Many applications really never will grow to use
anything but their default bundles, but they will be
none the worse for having gotten into the system.
Bear in mind that even if an application never needs to be
internationalized in the strictest sense, there can still be
advantages to having a string table in play.
Certain presentations button captions, phrasing of user
messages, etc. can be fine-tuned late in the development
process without difficulty, if message keys are used
consistently in the application.
Rev. 1.1
236
Jakarta Struts
Chapter 7
Lab 7
An Authentication Error Message
Introduction
In this lab you will enhance the existing Love is Blind logon page. It currently does
nothing to inform the user when authentication fails it is just shown repeatedly until the
user gets it right or goes away. You will add code to Logon.jsp to present an advisory
message, and a key and value to the default resource bundle to support the JSP.
Suggested Time: 30-45 minutes.
Root Directory:
OI\Struts
Directories:
Labs\Lab7
Examples\LoveIsBlind\Step7
Examples\LoveIsBlind\Step8
Files:
View\Member\Logon.jsp
Config\classes\application.properties
Instructions
1. Build and test the starter application. As you may already have noticed, if you flunk
your logon, the JSP just shows again, with password cleared. Its a pretty good guess
whats happening, from the users perspective, but its hardly good presentation.
2. Review the code in oi\LIB\action\Authenticate.java, and note that on failure it sets a
request-scope attribute authenticated to false.
3. Open Logon.jsp and add code right after the page heading. Start by using <c:if>
with a test attribute of ${authenticated == false} to see if authentication failed on
the recent request.
4. Inside the conditional tag, create a <b></b> element, and inside it a <fmt:bundle>
with attribute baseName set to application. Tags occurring inside this one will
default to look up keys in the application.properties bundle already deployed with
the application.
5. Finally, inside the bundle tag, add a <fmt:message> tag with key equal to
error.invalidLogon. This is a new key for the application, which will need to have
a value defined.
Rev. 1.1
237
Jakarta Struts
Chapter 7
6. Open application.properties and define a message for the new key something to
the effect that the user and password couldnt be authenticated in the member
database.
7. Build and retest the application, and provide a bad password for someone. You
should see your message clearly in the re-displayed logon page. (This is the Step8
answer code; the remaining steps are a quick experiment and are not reflected
anywhere in the example steps.)
Optional Steps
8. Try the same thing with <bean:message>, as follows.
9. You will need to declare the Struts bean tag library instead of the JSTL formatting
library.
10. You will still need the <c:if> conditional, but then you can place a <bean:message>
tag with the same key attribute where the <fmt:message> is currently.
11. You can remove the <fmt:bundle> entirely, since the new tag will automatically
look up the Struts default bundle as already configured.
Rev. 1.1
238