Using ActionErrors
Earlier in this chapter, we saw that the validate( )
method returned 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
.
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 a JSP custom tag that makes retrieving
and displaying the
error
messages very easy. This tag, called ErrorsTag
,
will be discussed in Chapter 8.
An instance of ActionErrors
can be instantiated in
the validate( )
method and populated by adding
instances of the ActionError
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){ ActionErrors errors = new ActionErrors( ); if( getEmail() == null || getEmail().length( ) < 1 ){ errors.add("email", new ActionError("security.error.email.required")); } if( getPassword() == null || getPassword().length( ) < 1 ){ errors.add("password", new ActionError("security.error.password.required")); } return errors; }
The validate( )
method in this fragment checks to make sure that the email and password ...
Get Programming Jakarta Struts 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.