Using ActionErrors
Earlier in this chapter in Example 7-2, we saw that
the validate()
method returns an
ActionErrors
object.
The ActionErrors
class encapsulates one or more
errors that have been discovered by the application. Each problem
discovered is represented by an instance of
org.apache.struts.action.ActionError
.
Tip
It should be pointed out that in Example 7-2,
instances of the ActionMessage
class and not
ActionError
were added to the
ActionErrors
collection.
ActionMessage
is actually the parent of
ActionError
. We will talk more about the usage of
the ActionMessage
class in the next section. For
now, when you see ActionError
mentioned, just know
that you can substitute ActionMessage
in most
cases.
An ActionErrors
object has request scope. Once an
instance is created and populated by the validate( )
method, it is stored into the request. Later, the JSP
page can retrieve the object from the request and use the
ActionError
objects contained within it to display
error messages to the user.
Tip
The Struts framework includes several JSP custom tags that make
retrieving and displaying the
messages
very easy. Two of them, ErrorsTag
and
MessagesTag
, are discussed in Chapter 8.
An instance of ActionErrors
can be instantiated in
the validate()
method and populated by adding
instances of the ActionMessage
class to it. The
LoginForm
from Example 7-2
demonstrated this and is illustrated again here for convenience:
public ActionErrors validate(ActionMapping mapping, HttpServletRequest request){ ...
Get Programming Jakarta Struts, Second Edition now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.