9.5. Reporting Errors and Messages from an Action
Problem
You want to display error messages to the user when things go wrong and success messages when things go right.
Solution
For Struts 1.1, use ActionErrors
for reporting
errors and ActionMessages
for informational
messages. For Struts 1.2, use ActionMessages
for
informational messages and errors.
Discussion
Struts gives you a least four classes and three custom tags for
creating, storing, and displaying errors and messages. In Struts 1.1,
the ActionErrors
class is used to hold a
collection of errors, represented by ActionError
instances. Each ActionError
consists of a key to
be looked up from the default MessageResources
bundle and an optional set of Object
s (up to four)
to be used as parameters for message substitution. The
Action
class in Example 9-9 shows
typical usage of ActionErrors
.
Example 9-9. Creating ActionErrors from an Action
package com.oreilly.strutsckbk.ch09; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.apache.commons.beanutils.PropertyUtils; import org.apache.struts.Globals; import org.apache.struts.action.Action; import org.apache.struts.action.ActionError; import org.apache.struts.action.ActionErrors; import org.apache.struts.action.ActionForm; import org.apache.struts.action.ActionForward; import org.apache.struts.action.ActionMapping; public class RegistrationAction extends Action { public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest ...
Get Jakarta Struts Cookbook 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.