1
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN""http://www.w3.org/TR/REC-html40/loose.dtd">
5
<!-- Generated by javadoc on Fri Aug 22 03:43:57 EDT 2003 -->
7
org.apache.struts.util (Apache Struts API Documentation)
9
<META NAME="keywords" CONTENT="org.apache.struts.util package">
10
<LINK REL ="stylesheet" TYPE="text/css" HREF="../../../../stylesheet.css" TITLE="Style">
15
parent.document.title="org.apache.struts.util (Apache Struts API Documentation)";
18
<BODY BGCOLOR="white" onload="asd();">
20
<!-- ========== START OF NAVBAR ========== -->
21
<A NAME="navbar_top"><!-- --></A>
22
<TABLE BORDER="0" WIDTH="100%" CELLPADDING="1" CELLSPACING="0">
24
<TD COLSPAN=3 BGCOLOR="#EEEEFF" CLASS="NavBarCell1">
25
<A NAME="navbar_top_firstrow"><!-- --></A>
26
<TABLE BORDER="0" CELLPADDING="0" CELLSPACING="3">
27
<TR ALIGN="center" VALIGN="top">
28
<TD BGCOLOR="#EEEEFF" CLASS="NavBarCell1"> <A HREF="../../../../overview-summary.html"><FONT CLASS="NavBarFont1"><B>Overview</B></FONT></A> </TD>
29
<TD BGCOLOR="#FFFFFF" CLASS="NavBarCell1Rev"> <FONT CLASS="NavBarFont1Rev"><B>Package</B></FONT> </TD>
30
<TD BGCOLOR="#EEEEFF" CLASS="NavBarCell1"> <FONT CLASS="NavBarFont1">Class</FONT> </TD>
31
<TD BGCOLOR="#EEEEFF" CLASS="NavBarCell1"> <A HREF="package-use.html"><FONT CLASS="NavBarFont1"><B>Use</B></FONT></A> </TD>
32
<TD BGCOLOR="#EEEEFF" CLASS="NavBarCell1"> <A HREF="package-tree.html"><FONT CLASS="NavBarFont1"><B>Tree</B></FONT></A> </TD>
33
<TD BGCOLOR="#EEEEFF" CLASS="NavBarCell1"> <A HREF="../../../../deprecated-list.html"><FONT CLASS="NavBarFont1"><B>Deprecated</B></FONT></A> </TD>
34
<TD BGCOLOR="#EEEEFF" CLASS="NavBarCell1"> <A HREF="../../../../index-all.html"><FONT CLASS="NavBarFont1"><B>Index</B></FONT></A> </TD>
35
<TD BGCOLOR="#EEEEFF" CLASS="NavBarCell1"> <A HREF="../../../../help-doc.html"><FONT CLASS="NavBarFont1"><B>Help</B></FONT></A> </TD>
39
<TD ALIGN="right" VALIGN="top" ROWSPAN=3><EM>
45
<TD BGCOLOR="white" CLASS="NavBarCell2"><FONT SIZE="-2">
46
<A HREF="../../../../org/apache/struts/upload/package-summary.html"><B>PREV PACKAGE</B></A>
47
<A HREF="../../../../org/apache/struts/validator/package-summary.html"><B>NEXT PACKAGE</B></A></FONT></TD>
48
<TD BGCOLOR="white" CLASS="NavBarCell2"><FONT SIZE="-2">
49
<A HREF="../../../../index.html" TARGET="_top"><B>FRAMES</B></A>
50
<A HREF="package-summary.html" TARGET="_top"><B>NO FRAMES</B></A>
55
document.writeln('<A HREF="../../../../allclasses-noframe.html" TARGET=""><B>All Classes</B></A>');
60
<A HREF="../../../../allclasses-noframe.html" TARGET=""><B>All Classes</B></A>
65
<!-- =========== END OF NAVBAR =========== -->
69
Package org.apache.struts.util
71
The Utilities package provides a variety of families of classes,
72
to solve problems that are commonly encountered in building web applications.
76
<A HREF="#package_description"><B>Description</B></A>
79
<TABLE BORDER="1" CELLPADDING="3" CELLSPACING="0" WIDTH="100%">
80
<TR BGCOLOR="#CCCCFF" CLASS="TableHeadingColor">
81
<TD COLSPAN=2><FONT SIZE="+2">
82
<B>Class Summary</B></FONT></TD>
84
<TR BGCOLOR="white" CLASS="TableRowColor">
85
<TD WIDTH="15%"><B><A HREF="ErrorMessages.html">ErrorMessages</A></B></TD>
86
<TD><B>Deprecated.</B> <I>Use org.apache.struts.action.ActionErrors instead</I></TD>
88
<TR BGCOLOR="white" CLASS="TableRowColor">
89
<TD WIDTH="15%"><B><A HREF="GenericDataSource.html">GenericDataSource</A></B></TD>
90
<TD><B>Deprecated.</B> <I>Use a <code>BasicDataSource</code> directly, or indirectly
91
acquire a data source provided by your container</I></TD>
93
<TR BGCOLOR="white" CLASS="TableRowColor">
94
<TD WIDTH="15%"><B><A HREF="ImageButtonBean.html">ImageButtonBean</A></B></TD>
95
<TD>A simple JavaBean to encapsulate the request parameters sent for an HTML
96
input element of type image.</TD>
98
<TR BGCOLOR="white" CLASS="TableRowColor">
99
<TD WIDTH="15%"><B><A HREF="IteratorAdapter.html">IteratorAdapter</A></B></TD>
100
<TD><B>Deprecated.</B> <I>Use commons-collections' IteratorUtils.asIterator(Enumeration).</I></TD>
102
<TR BGCOLOR="white" CLASS="TableRowColor">
103
<TD WIDTH="15%"><B><A HREF="LabelValueBean.html">LabelValueBean</A></B></TD>
104
<TD>A simple JavaBean to represent label-value pairs.</TD>
106
<TR BGCOLOR="white" CLASS="TableRowColor">
107
<TD WIDTH="15%"><B><A HREF="MessageResources.html">MessageResources</A></B></TD>
108
<TD>General purpose abstract class that describes an API for retrieving
109
Locale-sensitive messages from underlying resource locations of an
110
unspecified design, and optionally utilizing the <code>MessageFormat</code>
111
class to produce internationalized messages with parametric replacement.</TD>
113
<TR BGCOLOR="white" CLASS="TableRowColor">
114
<TD WIDTH="15%"><B><A HREF="MessageResourcesFactory.html">MessageResourcesFactory</A></B></TD>
115
<TD>Factory for <code>MessageResources</code> instances.</TD>
117
<TR BGCOLOR="white" CLASS="TableRowColor">
118
<TD WIDTH="15%"><B><A HREF="PropertyMessageResources.html">PropertyMessageResources</A></B></TD>
119
<TD>Concrete subclass of <code>MessageResources</code> that reads message keys
120
and corresponding strings from named property resources in the same manner
121
that <code>java.util.PropertyResourceBundle</code> does.</TD>
123
<TR BGCOLOR="white" CLASS="TableRowColor">
124
<TD WIDTH="15%"><B><A HREF="PropertyMessageResourcesFactory.html">PropertyMessageResourcesFactory</A></B></TD>
125
<TD>Factory for <code>PropertyMessageResources</code> instances.</TD>
127
<TR BGCOLOR="white" CLASS="TableRowColor">
128
<TD WIDTH="15%"><B><A HREF="RequestUtils.html">RequestUtils</A></B></TD>
129
<TD>General purpose utility methods related to processing a servlet request
130
in the Struts controller framework.</TD>
132
<TR BGCOLOR="white" CLASS="TableRowColor">
133
<TD WIDTH="15%"><B><A HREF="ResponseUtils.html">ResponseUtils</A></B></TD>
134
<TD>General purpose utility methods related to generating a servlet response
135
in the Struts controller framework.</TD>
137
<TR BGCOLOR="white" CLASS="TableRowColor">
138
<TD WIDTH="15%"><B><A HREF="ServletContextWriter.html">ServletContextWriter</A></B></TD>
139
<TD>A PrintWriter implementation that uses the logging facilities of a
140
<code>javax.servlet.ServletContext</code> to output its results.</TD>
142
<TR BGCOLOR="white" CLASS="TableRowColor">
143
<TD WIDTH="15%"><B><A HREF="StrutsValidator.html">StrutsValidator</A></B></TD>
144
<TD><B>Deprecated.</B> <I>As of Struts 1.1, replaced by <A HREF="../../../../org/apache/struts/validator/FieldChecks.html"><CODE>FieldChecks</CODE></A></I></TD>
146
<TR BGCOLOR="white" CLASS="TableRowColor">
147
<TD WIDTH="15%"><B><A HREF="StrutsValidatorUtil.html">StrutsValidatorUtil</A></B></TD>
148
<TD><B>Deprecated.</B> <I>As of Struts 1.1, replaced by <A HREF="../../../../org/apache/struts/validator/Resources.html"><CODE>Resources</CODE></A></I></TD>
150
<TR BGCOLOR="white" CLASS="TableRowColor">
151
<TD WIDTH="15%"><B><A HREF="TokenProcessor.html">TokenProcessor</A></B></TD>
152
<TD>TokenProcessor is responsible for handling all token related functionality.</TD>
159
<TABLE BORDER="1" CELLPADDING="3" CELLSPACING="0" WIDTH="100%">
160
<TR BGCOLOR="#CCCCFF" CLASS="TableHeadingColor">
161
<TD COLSPAN=2><FONT SIZE="+2">
162
<B>Exception Summary</B></FONT></TD>
164
<TR BGCOLOR="white" CLASS="TableRowColor">
165
<TD WIDTH="15%"><B><A HREF="AppException.html">AppException</A></B></TD>
166
<TD><B>Deprecated.</B> <I>use <A HREF="../../../../org/apache/struts/util/ModuleException.html"><CODE>ModuleException</CODE></A></I></TD>
168
<TR BGCOLOR="white" CLASS="TableRowColor">
169
<TD WIDTH="15%"><B><A HREF="ModuleException.html">ModuleException</A></B></TD>
170
<TD>Used for specialized exception handling.</TD>
176
<A NAME="package_description"><!-- --></A><H2>
177
Package org.apache.struts.util Description
181
The Utilities package provides a variety of families of classes,
182
to solve problems that are commonly encountered in building web applications.
184
<a name="doc.Description"></a>
186
<a href="#doc.Intro">[Introduction]</a>
187
<a href="#doc.Beans">[Beans]</a>
188
<a href="#doc.Utilities">[Servlet Utilities]</a>
189
<a href="#doc.Messages">[Message Resources]</a>
194
<a name="doc.Intro"></a>
195
<h3>Introduction</h3>
197
<p>The Struts Utilities Package offers several families
198
of classes that assist in solving commonly encountered problems when building
199
web applications. Most of the classes in this package do not rely on the
200
controller servlet framework, or the custom tag libraries, so they are also
201
suitable for general Java application programming. The following families
204
<li><a href="#doc.Beans">Beans</a> - A small set of utility beans useful for
205
encapsulating form elements.</li>
206
<li><a href="#doc.Utilities">Servlet Utilities</a> - A set of classes useful
207
for working with servlet-related classes.</li>
208
<li><a href="#doc.Messages">Message Resources</a> - A family of classes that
209
features access to internationalized message strings based on a message
210
key coupled with a <code>java.util.Locale</code> object representing a
211
particular user's preferred language.</li>
216
<a name="doc.Beans"></a>
219
<p> The <code>ImageButtonBean</code> is a simple JavaBean to encapsulate the
220
request parameters sent for an HTML input element of type image. The
221
<code>LabelValueBean</code> is a simple JavaBean to represent label-value
222
pairs, especially useful for html option elements.
227
<a name="doc.Utilities"></a>
228
<h3>Servlet Utilities</h3>
230
<p> The <code>RequestUtils</code> is a general purpose utility methods
231
related to processing a servlet request. The <code>ResponseUtils</code>
232
is a general purpose utility methods related to generating a servlet
233
response. The <code>ServletContextWriter</code> is a PrintWriter
234
implementation that uses the logging facilities of a
235
<code>javax.servlet.ServletContext</code> to output its results.
240
<a name="doc.Messages"></a>
241
<h3>Message Resources</h3>
245
<p>Modern applications often include the requirement to support multiple
246
languages, for users who prefer to interact in a language other than the
247
default language configured on the server platform. In addition, sentences
248
often need to be constructed, with dynamic content whose placement in the
249
message depends on the standard sentence structure in that particular
252
<p>The standard Java platform includes a family of classes
253
(<code>java.util.ResourceBundle</code>) designed to support looking up message
254
strings based on a standard "key". The resource bundle classes automatically
255
access a Java class (or properties file) that is named with a naming
256
convention that includes the Locale to which messages in that class (or file)
257
pertain. However, this selection is based only on the default Locale of the
258
server platform, and cannot be adjusted on a per-user basis as required for an
259
internationalized web application.</p>
261
<p>Struts includes a family of classes
262
(<code>org.apache.struts.util.MessageResources</code>) that extends the basic
263
approach to looking up message strings by key, allowing you to optionally
264
specify a Locale along with the key. In this way, you can build applications
265
that let your users select which Locale they wish to operate within, and then
266
look up messages in that language - using the same message keys no matter what
267
language is selected.</p>
269
<p>In addition to supporting dynamic selection of a Locale for message lookup,
270
the <code>MessageResources</code> family of classes optionally allow you to
271
specify up to four parameter replacement objects, which are used to replace the
272
parameter placeholders "{0}" through "{3}" in the retrieved message. This
273
replacement uses the facilities of the standard Java
274
<code>java.text.MessageFormat</code> class, which supports many extended
275
formatting capabilities as well.</p>
277
<p>For more information about internationalized messages, consult the following
278
resources in your Java Development Kit documentation bundle:</p>
280
<li><em>Internationalization Info</em> - General information on Java's standard
281
support for internationalized applications can be found at
282
<code><$JAVA_HOME/docs/guide/internat/index.html></code>.
283
The "Internationalization Overview" section includes useful information
284
about Locales, localized resources, message formatting, and other
285
relevant topics.</li>
286
<li><em>Internationalization Tutorial</em> - The Java Language Tutorial has a
287
comprehensive trail covering internationalization, available at:
288
<a href="http://java.sun.com/docs/books/tutorial/i18n/index.html">
289
http://java.sun.com/docs/books/tutorial/i18n/index.html</a>.</li>
290
<li><em>Javadoc APIs</em> - You will want to consult the Javadoc API
291
documentation for the following standard Java classes:
293
<li><code>java.text.MessageFormat</code>
294
<li><code>java.util.ResourceBundle</code>
295
<li><code>java.util.PropertyResourceBundle</code>
296
<li><code>java.util.Properties</code> - See the documentation for the
297
<code>load()</code> method for the valid syntax of properties files
298
that you prepare.</li>
302
<h5>Using the Standard MessageResources Implementation</h5>
304
<p>The standard <code>MessageResources</code> implementation provided by the
305
Struts library uses Java properties files to initialize message strings, in a
306
manner very similar to that supported by the
307
<code>java.util.PropertyResourceBundle</code> class. The following steps are
308
required to use these facilities in your Java application.</p>
310
<p>First, prepare a Java properties file for each language (or Locale) in which
311
you wish to support your messages. The filenames you use must conform to the
312
naming convention for property resource bundles, as described in the
313
documentation referenced above. Be sure you use the same message keys in each
314
file to identify the same message.</p>
316
<p>For example, you might prepare files in French, Spanish, and English that
317
contain language-specific versions of the word "Hello". The French file would
318
be named <code>Messages_fr.properties</code> and contain the following:</p>
322
<p>while the Spanish and English files would be named
323
<code>Messages_es.properties</code> and <code>Messages_en.properties</code>
324
respectively. The corresponding message string definitions would say
325
<code>hi=Hola</code> and <code>hi=Hello</code> in these files.</p>
327
<p>Second, place these properties files into the class path for your
328
application, exactly as you would with class files themselves. The name
329
actually used to load resources will look like a fully qualified Java class
330
name (with appropriate package prefixes), so the file should be nested inside
331
a directory structure that matches the packaging (either in an unpacked
332
directory, or within a JAR file, as appropriate). For example, assume you
333
place directory "foo" on your classpath, and stored the above properties files
334
in directory "foo/com/mycompany/mypackage". (If you were using a JAR file like
335
"foo.jar" instead, the files would be in directory "com/mycompany/mypackage"
336
within the JAR file).
338
<p>Third, initialize a <code>MessageResources</code> object that corresponds
339
to the set of properties files for a particular name, within a particular
340
package. The easiest way to do this is to initialize a variable in your main
341
application class, like this:</p>
343
public static MessageResources messages =
344
MessageResources.getMessageResources("com.mycompany.mypackage.Messages");
347
<p>Note that the "com.mycompany.mypackage" part of the name matches the package
348
directory into which you placed your properties files, and "Messages" is the
349
filename prefix for the particular family of properties files supported by this
350
<code>MessageResources</code> instance. Depending on your development process,
351
you might find it convenient to store all message strings for an entire
352
application in a single properties file family, or to have several families -
353
in Struts, for example, there is a family of properties files for each Java
356
<p>To access a message string with a particular Locale, execute a statement
359
Locale locale = ... select the locale to be used ...
360
String message = messages.getMessage(locale, "hi");
363
<p>In this case, the variable <code>message</code> will contain the message
364
string corresponding to the key "hi", in the language that corresponds to the
365
locale that was selected.</p>
367
<p>For an example of message formatting with replaceable parameters, assume
368
that the message strings looked like this, instead (only the English version
369
is shown - corresponding changes would be made in the other files):</p>
374
<p>Now, you can personalize the retrieved message like this:</p>
376
Locale locale = ... select the locale to be used ...
378
String message = messages.getMessage(locale, "hi", name);
381
<p>and the marker "{0}" will have been replaced by the specified name (Joe),
382
no matter which language is in use. See the JavaDoc API documentation for the
383
<code>java.text.MessageFormat</code> class for more advanced uses of the
384
parameter replacement mechanism.</p>
386
<h5>Developing Your Own MessageResources Implementation</h5>
388
<p>In the above example, we were using the default
389
<code>MessageResources</code> implementation supplied by Struts, which uses
390
property files to store the message strings. It is also possible to create
391
customized mechanisms to retrieve messages (such as loading them on demand
392
from a database). The steps required are as follows:</p>
394
<li>Create a customized subclass of
395
<code>org.apache.struts.util.MessageResources</code> that implements
396
message lookup operations as you require.</li>
397
<li>Create a customized subclass of
398
<code>org.apache.struts.util.MessageResourcesFactory</code> that will
399
create an instance of your custom <code>MessageResources</code> class
400
when the <code>createResources</code> method is called. Note that the
401
"config" argument to this method can be used to select families of
402
messages in any manner appropriate to your needs - you are not required
403
to emulate the "fully qualified Java class name" approach that is used
404
by the standard <code>PropertyMessageResourcesFactory</code> class.</li>
405
<li>Tell the <code>MessageResourcesFactory</code> class the name of the
406
customized <code>MessageResourcesFactory</code> implementation to use
407
when creating new factory instances.</li>
408
<li>Create a new factory instance.</li>
409
<li>Ask the new factory instance to create a <code>MessageResources</code>
410
instance for you.</li>
413
<p>A code example that illustrates this technique is:</p>
415
MessageResourcesFactory.setFactoryClass("com.mycompany.mypkg.MyFactory");
416
MessageResourcesFactory factory = MessageResourcesFactory.createFactory();
417
MessageResources resources =
418
factory.createResources("configuration information");
421
<p>Once you have created your custom MessageResources instance, you utilize it
422
to access message strings (with or without parameter replacement objects),
423
exactly as we illustrated with the standard implementation in the previous
426
<h5>Using MessageResources With Struts</h5>
428
<p>If your application uses the Struts controller servlet, you can optionally
429
configure Struts to load an application-specific message resources instance for
430
you, and make it available as a servlet context attribute (in JSP terms, an
431
application-scope bean). This mechanism is managed by setting the following
432
servlet initialization parameters in the web application deployment descriptor:
435
<li><strong>application</strong> - The configuration string that will be
436
passed to the <code>createResources()</code> method of the message
437
resources factory, in order to identify the family of resources to be
438
supported. If you use the standard message resources factory, this must
439
be the base fully qualified name of the property resources files used
440
to contain these messages, as illustrated above.</li>
441
<li><strong>factory</strong> - Fully qualified Java class name of the
442
<code>MessageResourcesFactory</code> to be used. By default, the standard
443
implementation provided by Struts
444
(<code>org.apache.struts.util.PropertyMessageResourcesFactory</code>)
448
<p>Struts provides several JSP custom tags that assume the existence of a
449
<code>java.util.Locale</code> attribute in the user's session, under the key
450
named by the constant string value of <code>Action.LOCALE_KEY</code>. Your own
451
application logic can set this attribute at any time, or you can ask Struts to
452
set it automatically (if not already set) based on the
453
<code>Accept-Language</code> HTTP header included with the request. There are
454
two mechanisms by which you request Struts to perform this service:</p>
456
<li>To have this service performed on every request submitted to the controller
457
servlet, set the servlet initialization parameter <code>locale</code> to
458
the value <code>true</code> in the application deployment descriptor.</li>
459
<li>To have this service performed by a JSP page when it is accessed directly
460
by a user, utilize a <code><form:html ... locale="true" ... /></code>
461
tag at the top of each page.</li>
468
<!-- ========== START OF NAVBAR ========== -->
469
<A NAME="navbar_bottom"><!-- --></A>
470
<TABLE BORDER="0" WIDTH="100%" CELLPADDING="1" CELLSPACING="0">
472
<TD COLSPAN=3 BGCOLOR="#EEEEFF" CLASS="NavBarCell1">
473
<A NAME="navbar_bottom_firstrow"><!-- --></A>
474
<TABLE BORDER="0" CELLPADDING="0" CELLSPACING="3">
475
<TR ALIGN="center" VALIGN="top">
476
<TD BGCOLOR="#EEEEFF" CLASS="NavBarCell1"> <A HREF="../../../../overview-summary.html"><FONT CLASS="NavBarFont1"><B>Overview</B></FONT></A> </TD>
477
<TD BGCOLOR="#FFFFFF" CLASS="NavBarCell1Rev"> <FONT CLASS="NavBarFont1Rev"><B>Package</B></FONT> </TD>
478
<TD BGCOLOR="#EEEEFF" CLASS="NavBarCell1"> <FONT CLASS="NavBarFont1">Class</FONT> </TD>
479
<TD BGCOLOR="#EEEEFF" CLASS="NavBarCell1"> <A HREF="package-use.html"><FONT CLASS="NavBarFont1"><B>Use</B></FONT></A> </TD>
480
<TD BGCOLOR="#EEEEFF" CLASS="NavBarCell1"> <A HREF="package-tree.html"><FONT CLASS="NavBarFont1"><B>Tree</B></FONT></A> </TD>
481
<TD BGCOLOR="#EEEEFF" CLASS="NavBarCell1"> <A HREF="../../../../deprecated-list.html"><FONT CLASS="NavBarFont1"><B>Deprecated</B></FONT></A> </TD>
482
<TD BGCOLOR="#EEEEFF" CLASS="NavBarCell1"> <A HREF="../../../../index-all.html"><FONT CLASS="NavBarFont1"><B>Index</B></FONT></A> </TD>
483
<TD BGCOLOR="#EEEEFF" CLASS="NavBarCell1"> <A HREF="../../../../help-doc.html"><FONT CLASS="NavBarFont1"><B>Help</B></FONT></A> </TD>
487
<TD ALIGN="right" VALIGN="top" ROWSPAN=3><EM>
493
<TD BGCOLOR="white" CLASS="NavBarCell2"><FONT SIZE="-2">
494
<A HREF="../../../../org/apache/struts/upload/package-summary.html"><B>PREV PACKAGE</B></A>
495
<A HREF="../../../../org/apache/struts/validator/package-summary.html"><B>NEXT PACKAGE</B></A></FONT></TD>
496
<TD BGCOLOR="white" CLASS="NavBarCell2"><FONT SIZE="-2">
497
<A HREF="../../../../index.html" TARGET="_top"><B>FRAMES</B></A>
498
<A HREF="package-summary.html" TARGET="_top"><B>NO FRAMES</B></A>
503
document.writeln('<A HREF="../../../../allclasses-noframe.html" TARGET=""><B>All Classes</B></A>');
508
<A HREF="../../../../allclasses-noframe.html" TARGET=""><B>All Classes</B></A>
513
<!-- =========== END OF NAVBAR =========== -->
516
Copyright � 2000-2003 - Apache Software Foundation