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