I am using Struts and Spring for my application, with the DelegatingActionProxy to allow my Struts actions to be injected with dependencies by Spring.
I have a Struts custom exception handler which I would also like to inject with dependencies from Spring but am not sure how.
My Struts config file contains similar to the following (application-specific clutter removed):
and the exception handler currently just uses log4j to log the exception before forwarding to the path above:
Please could someone point me in the right direction to allow me to inject Spring-managed objects as dependencies of MyStrutsExceptionHandler? This would be a first step to handling such errors more intelligently, e.g. with versions for storing to DB / sending email / etc.
Thanks, Simon [ September 22, 2006: Message edited by: Simon Baker ]
Joined: Sep 09, 2004
I have a workaround where a Struts exception handler places the exception onto a request attribute and then the "path" attribute directs flow to a Struts Action (configured in Spring using the DelegatingActionProxy now that it's simply an Action). The action then delegates to (POJO) exception handlers as injected via core Spring. The forward of that Action then goes to my global error JSP.
This works functionally but seems a bit messy - does anyone know of a cleaner Spring proxy for exception handlers?
I was able to Spring-enable my ExceptionHandlers, but it was a bit ugly. There were two technical issues:
How to get a reference to the ServletContext and the WebApplicationContext stored within it
How to determine the name of the ExceptionHandler bean within the Spring application context
We use a custom Struts RequestProcessor, so I solved (1) by having the RequestProcessor copy the WebApplicationContext from the ServletContext into a well-known request attribute (for all requests). If you have a custom Action superclass I think this could be done there instead.
I solved (2) by using the fully-qualified class name of the exception being handled (ExceptionConfig.getType()), appended to a "ExceptionHandler:" prefix as the bean name. For example: "ExceptionHandler:java.lang.Exception".
I created a DelegatingExceptionHandlerProxy class, and that's what I configure for the "handle" attribute of the <exception> in my struts-config.xml file. Its code is pretty straightforward.
[ December 27, 2006: Message edited by: Chris Mair ]
subject: Struts ExceptionHandler - configure using Spring