In the Struts 1.2.4 release, ActionError is deprecated. ActionErrors would be deprecated, except that it is part of the method signature for ActionForm.validate().

After Struts 1.1, the preferred way to handle errors in your Java code is:

 
   ActionMessages am = new ActionMessages(); 
   am.add( ActionMessages.GLOBAL_MESSAGE,  
           new ActionMessage( "not.authorized.for.account" ) ); 
   saveErrors( request, am ); 
 

and to display those messages in the JSP:

 
   <html-el:messages id="msg" message="false"> 
      <c:out value="${msg}"/> 
   </html-el:messages> 
 

or if you are not using the struts-el tags:

 
   <html:messages id="msg" message="false"> 
      <bean:write name="msg" /> 
   </html:messages> 
 

This change was motivated by the realization that there are non-error messages that need to be communicated. Action now has an additional method saveMessages(request,ActionMessages), which saves the messages in request scope under a different key, Globals.MESSAGE_KEY. To display messages saved with saveMessages:

 
   <html-el:messages id="msg" message="true"> 
      <c:out value="${msg}"/> 
   </html-el:messages> 
 

Finally, you can save an ActionMessages object under an arbitrary key and display it with the html:messages tag:

 
  ActionMessages am = new ActionMessages(); 
   am.add( ActionMessages.GLOBAL_MESSAGE,  
           new ActionMessage( "msg.warning" ) ); 
  request.setAttribute("warnings", am); 
 

and in your page:

 
   <html-el:messages name="warnings" id="msg"> 
      <c:out value="${msg}"/> 
   </html-el:messages> 
 

Of course, see the full html:messages doc for details.

Comment from hauser@acm.org: So what do you recommend for somebody who already wants to use ActionMessages, but also has ValidatorForm?

Response: use ActionMessages anywhere except in an ActionForm. Soon we will provide an alternate method on ActionForm which returns an ActionMessages and lets us move forward with fully deprecating ActionErrors.

Comment from Niall Pemberton: ''This page implies that the <html:messages> tag is the preferred way to handle messages - I don't agree with that. Wether you use <html:messages> or <html:errors> is just a matter of preference - see here for a comparison.

  • No labels