Use Exception handlers
Since Action classes tend to access the same business layer, most Actions often need to catch the same set of exceptions. Rather than sprinkle Actions with nearly identical try..catch blocks, configure an Exception handler to catch whatever exceptions an Action may throw.
<struts> <global-results> <result name="error">/Error.jsp</result> </global-results> <global-exception-mappings> <exception-mapping result="error" exception="java.lang.Throwable"/> </global-exception-mappings> <!-- ... --> </struts>Exception handlers can be either global or local to an action mapping.
<struts> <action name="Login" class="actions.Login"> <!-- ... --> <result name="expired" type="chain"> ChangePassword </result> <exception-mapping exception="dao.ExpiredPasswordException" result="expired"/> </action> <!-- ... --> </struts>Use of exception handlers separates concerns and reduces redundant code. Each Action has fewer lines of code to maintain, and we know that exceptions are being handled consistently.
HTH, Ted.
Ted Husted is a software engineer and team mentor. His specialty is building agile web applications with open source products like Yahoo! User Interface Library, Struts, Spring, iBATIS, and MySQL, for either Java or Microsoft .NET, and helping others do the same.
Copyright Ted Husted 2007. All rights reserved.