Spring MVC: Exception Handling

I was trying to figure out how to use error pages with Spring. Now this turned out to be quite a hassle. I will just wite down some cases which I came across. Before you might want to have a look at this detailed article: http://doanduyhai.wordpress.com/2012/05/06/spring-mvc-part-v-exception-handling/.

Tiles definition not found

I am using tiles. When no Tiles defintion is found in my case a JSTLView is being built. If this goes wrong, a 404 response is sent which can be picked up by the servlet container using the following entry in the web.xml:

<error-page>
	<error-code>404</error-code>
	<location>/error/404</location>
</error-page>

Tiles: file not found (org.apache.tiles.util.TilesIOException)

If some file in a Tiles template definition cannot be found, a javax.servlet.ServletException is raised. This will properly send a 500 error response which can be picked up by an entry in the web.xml:

<error-page>
	<error-code>500</error-code>
	<location>/error/500</location>
</error-page>

Arbitrary Exception in Controller

An arbitrary exception inside a controller method also seems to trigger the generation of an error code response which can be picked up by the entry in the web.xml. The error code is 500.

Immportant: Yet I was building an error page using Tiles again. Now, if the error page cannot be built and an exception is raised again, the servlet container will not use the entry in the web.xml a second time preventing infinite loops. I do not know how this happens (or even if this is actually a feature - it seems so though), but it seems pretty smart, no?

VN:F [1.9.22_1171]
Rating: 0.0/10 (0 votes cast)
VN:F [1.9.22_1171]
Rating: 0 (from 0 votes)

Leave a Reply