Win a copy of Svelte and Sapper in Action this week in the JavaScript forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Bear Bibeault
  • Junilu Lacar
  • Jeanne Boyarsky
  • Tim Cooke
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • salvin francis
  • Frits Walraven
  • Scott Selikoff
  • Piet Souris
  • Carey Brown

Struts ExceptionHandler - configure using Spring

Ranch Hand
Posts: 57
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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.

[ September 22, 2006: Message edited by: Simon Baker ]
Simon Baker
Ranch Hand
Posts: 57
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
    With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
      Bookmark Topic Watch Topic
    • New Topic