Exception-handling in Struts tools 15649 6401

Exception-handling in Struts

For a simple and generic solution to the boring and code-aesthetics ruining routine of catching rare exceptions in your Struts-actions, try a global exceptionhandler.

1) In struts-config.xml:

    <exception type="yourpackage.YourDAOException"

2) Add an entry in your ApplicationResources.properties. Something like:
error.dao.generic = There was a problem accessing the database

3) Create an errorpage with <html :errors/> somewhere in the body

Also, you could add handler="yourpackage.ActionExceptionHandler" to the exception declaration in struts-config. Then, create this handler class by extending org.apache.struts.action.ExceptionHandler and write some code that logs the exceptions or does other useful things.

Now, everytime you need to write a try-catch block for a YourDAOException, and all you do in it is adding some errors to the ActionErrors object and forwarding to a ‘failure-page’, you can just let the globalexception-thingy do it’s job.

One Response

  1. Abhishek January 7, 2005