| Author |
Question in Spring Exception Resolver
|
Vinayak patil
Ranch Hand
Joined: Aug 30, 2005
Posts: 67
|
|
Hi, In my action-servlet.xml file I am specifying the exceptionResolver bean as follows: In my controller class, I am throwing an IOException I was expecting it to go the "Error" page as java.lang.Exception is specified before java.io.IOException in the mapping. But it redirects to the "IOEx" page. If I comment out the entry for IOException in action-servlet.xml, it redirects to the "Error" page. I would like to know how does Spring manage to do this. Does the order of the entry in the bean does not matter? Thanks in advance, Vinayak
|
"I can resist everything except temptation"
|
 |
Ken Krebs
Ranch Hand
Joined: Nov 27, 2002
Posts: 451
|
|
|
When defining your exception handling, you are simply defining inline an instance of java.util.Properties which essentially is a Hashtable for looking up an object via another object which is acting as the key. The ordering of the definitions is irrelevant.
|
kktec<br />SCJP, SCWCD, SCJD<br />"What we observe is not nature itself, but nature exposed to our method of questioning." - Werner Heisenberg
|
 |
Christophe Verré
Marshal
Joined: Nov 24, 2005
Posts: 14361
|
|
As Ken said, the order is irrelevant. Spring will only look at the exception type, look for it in the map, and redirect to the specified location if found.
|
[SCBCD Wall of Fame] [My Blog]
All roads lead to JavaRanch
Help Japan. Make a donation.
|
 |
Vinayak patil
Ranch Hand
Joined: Aug 30, 2005
Posts: 67
|
|
Hi, Thanks for the responses. I still have a doubt with the behaviour though. I modified the bean definition so that it looks like: (I removed the entry for IOException) Now it redirects to Error page. So my understanding is this: When an exception occurs, Spring will search for the exception type in the "exceptionMappings" property. If it does not find, it tries to search a generic exception type defined and if so, will redirect it to that page. Thats why, though java.io.IOException was not mapped, it was able to redirect it to the "Error" page. Is my understanding correct? -Vinayak
|
 |
Ken Krebs
Ranch Hand
Joined: Nov 27, 2002
Posts: 451
|
|
From the javadoc for SimpleMappingExceptionResolver.setExceptionMappings:
setExceptionMappings public void setExceptionMappings(Properties mappings) Set the mappings between exception class names and error view names. The exception class name can be a substring, with no wildcard support at present. A value of "ServletException" would match javax.servlet.ServletException and subclasses, for example. NB: Consider carefully how specific the pattern is, and whether to include package information (which isn't mandatory). For example, "Exception" will match nearly anything, and will probably hide other rules. "java.lang.Exception" would be correct if "Exception" was meant to define a rule for all checked exceptions. With more unusual exception names such as "BaseBusinessException" there's no need to use a FQN.
That should clarify things. When in doubt, look in the docs and/or write a test.
|
 |
 |
|
|
subject: Question in Spring Exception Resolver
|
|
|