Bech on Enterprise Java
Monday, January 30, 2006
  Struts: Going from 1.1 to 1.2 error handling
Assigned to upgrade some code for my current client I discover that the way to do error handling and display error messages has changed between version 1.1 and 1.2 of the Struts framework

There has definitly been some deprecations

- add(String, ActionError) in the ActionErros classs is deprecated.
- saveErrors(HttpServletRequest, ActionErrors) in the Action class is deprectated.

The ActionErrors class itself, however, is not deprecated. It seems that there are too many internal dependencies to this class. But the contributors would like to remove it, so it will probably disappear in future releases.

The ActionError and ActionErros scheme is now replaced with ActionMessage and ActionMessages.

After doing some reading, it seems the html:messages tag was introduced because people didn't like the fact that using html:errors required HTML markup in the message resources. Using html:messages removes this requirement.

I found this very good article describing migration on the Struts Wiki. Since there was no mention of this in the "official" developer guide, I guess that more and more informatio will be published through the Wiki. Maybe someone should copy the entire development guide over there.

The upgrade guide can be found here:
http://wiki.apache.org/struts/StrutsUpgradeNotes11to124
Notes on the deprecations on error handling can be found here:
http://wiki.apache.org/struts/StrutsDeprecatedActionErrors
A Practical example on how-to do it can be found here
http://www.niallp.pwp.blueyonder.co.uk/HelpTagsErrorsAndMessages.html

So... In summary Old Code like this:

ActionErrors errors = new ActionErrors();
errors.add(ActionErrors.GLOBAL_MESSAGE, new ActionError("error.user.internalerror"));
saveErrors(request, errors);


Has to be replaced with code like this

ActionMessages errors = new ActionMessages();
errors.add(ActionMessages.GLOBAL_MESSAGE, new ActionMessage("error.user.internalerror", e.getMessage(), e));
saveErrors(request, errors);

And the JSP code that used to look like this ...

<html:errors />

(Where you had to specify header, footer, prefix and suffix for error messages like this)

errors.header=<h3><font color="red">Validation Errors</font></h3><ul>errors.footer=</ul>
errors.prefix=<li>
errors.suffix=</li></em>

... Is now replaced with JSP code like this ;

<logic:messagesPresent>
<h3><font color="red"><bean:message key="myForm.error.header" /></font></3>
<ul>
 <html:messages id="msg">
  <li>bean:write name="msg" /></li>
 </html:messages>
</ul>
</logic:messagesPresent>

A change for the better if you ask me, Let's hope the Struts guys will put all the documentation in one place soon, and take out that last ActionErrors class that still isn't deprecated.

 
I hereby promise to blog my thoguhts and views on Enterprise java, design patterns, frameworks and all other things that make life as a software developer interesting.

Name:
Location: Oslo, Oslo, Norway

www.glennbech.com

ARCHIVES
May 2005 / June 2005 / July 2005 / January 2006 / February 2006 / June 2006 / July 2006 / August 2006 / October 2006 / November 2006 / March 2007 /


Powered by Blogger