Many Struts Applications Can Get by with a Simple
. Tag at the top of a page If any error messages are present, it's this tag's job to print them all out To help prettify the output,
Errors.Header =
Errors.footer = ul>
Struts 1.0 developers can then include
Errors.Header =
Errors.Footer UL>
Errors.PREFIX =
Errors.suffix = li>
In Your Application Resources, and The (Struts 1.1)
Though, many purists would complain that HTML markup has no place in a message resources file. And they would right. Even with the Struts 1.1 prefix and suffix feature, you may still need to use different markup different pages.
For Struts 1.0 applications, the Struts Validation extension (see the Struts Resource page) offers a useful alternative to the standard.
Validator: Errors>
Ul>
Validator: Errorsexist>
In struts 1.1, the tags were adopted Into the core taglibs. Here's The Same Example Using The Struts 1.1 Rendition.
html: Messages>
Ul>
logic: MessagesPresent>
This is all great if you just want to print your messages as a batch. But many messages are related to data-entry validation and involve a specific field. Many page designs expect a message concerning a field to printed next to a field.
NOT A Problem. When the error message is queued, you can to go with it. If you don't specify a property (using any of the tags we described), the all the message print. If you do Specify a Property, Then Only The Messages Queued for That Property print.
The default code for queuing an error message is:
Errors.Add (
ActionerRORS.GLOBAL_ERROR,
New ActionError ("Error.username.Required)
);
To Specify That this message is for the "username" Property, We Would Code this instead: errors.add (
"Username", New Actionerror ("Error.userName.Required)));
IF WE Specify A Property, We can use the
Username:
password:
The "Username" Errors Print next to the username field, and any "password" Errors Print Next to the Password Field.
But what if you need to print Both specific and general errors?
. Again, no problem You can also specify the "generic" property just like you did in the Java code First, at the top of your JavaServer Page import the Action and ActionErrors package so you can reference the appropriate constants.:
<% @ page import = "org.apache.struts.Action.action"%>
<% @ page import = "org.apache.struts.Action.Actionerror"%>
The THE TAGS, USE A Runtime Expression to Specify The Constants:
logic: present>
Viola! Specific Messages Print Out In Specific Places, And Any "General" Errors CAN Still Print Out in A Place of their OWN.
Of course, you do not have to settle for any of these standard tags. If these variations still do not meet your specific needs, take a peek at the source code and cobble up your own! The framework provides the queue, but how IT Prints is up to you.
Hth, TED.