Exception-handling in Struts

Jasper 1

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.
Example:

1) In struts-config.xml:

<global-exceptions>
    <exception type="yourpackage.YourDAOException"
         key="error.dao.generic"
         path="/error.jsp"/>
</global-exceptions>

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 thought on “Exception-handling in Struts

  1. This description doesn’t describe how we can call ExceptionHandler from our Action.In my case DatabaseExceptionHandler is not getting called.Its the ActionErrors and the return mapping.findForward(“exception”) which is helping to forward to the respective page.

Comments are closed.

Next Post

Core J2EE patterns, Abstract Factory: unit testing with Struts

Facebook0TwitterLinkedinDuring development of your Struts-based web tier, you want to be able to test your page flow, action forms and validation independent of your persistency layer, which may be implemented with any Object Relational framework. That is were the abstract factory design pattern comes in handy. Suppose we want to […]