Configuring Struts Exception Handler

 < Day Day Up > 



Configuring Struts' Exception Handler

Using Struts' declarative exception-handling mechanism is very simple. All you have to do is add exception-handler definitions to the struts-config.xml file. Exception-handler definitions work in much the same way as forward definitions. You can create global exception-handler definitions and/or create action-specific handlers. Each exception-handler definition specifies the exception that it is to handle, a key to an error message in the ApplicationResources.properties resource bundle, and the path to a page to forward to when the exception is caught by the handler. Following is an example global exception-handler definition:

<global-exceptions>   <exception     type="com.jamesholmes.minihr.NoResultsFoundException"      key="error.NoResultsFoundException"     path="/exception.jsp"/> </global-exceptions>

The type attribute specifies the fully qualified class name of the exception that the handler is for. When an exception of this type is thrown, the exception handler will take control and process the exception. The key attribute specifies the name of a key in the ApplicationResources.properties file that will be used to populate an ActionError object that will be passed to the page specified by the path attribute. The path attribute specifies the path to a page that will be forwarded to by the exception handler when the specified exception type is thrown. The page can then display the error message placed in the ActionError object, if so desired.

The second way you can define exception handlers is by nesting them inside action definitions, as shown next:

<!-- Action Mappings Configuration --> <action-mappings>   <action path="/search"           type="com.jamesholmes.minihr.SearchAction"           name="searchForm"           scope="request"           validate="true"           input="/search.jsp">     <exception       type="com.jamesholmes.minihr.NoResultsFoundException"        key="error.NoResultsFoundException"       path="/exception.jsp"/>   </action> </action-mappings>

Action-nested exception handlers handle the specified exception only if it is thrown by the enclosing action. If the exception is thrown by another action, it will not be handled.

You don't have to modify your Action classes in any way to take advantage of declarative exception handling. However, if you already have try-catch blocks for exceptions that you want handled by the exception handler, you need to remove them so that the exception gets propagated up the chain for the exception handler to process.



 < Day Day Up > 



Struts. The Complete Reference
Struts: The Complete Reference, 2nd Edition
ISBN: 0072263865
EAN: 2147483647
Year: 2003
Pages: 134
Authors: James Holmes

Similar book on Amazon

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net