tag:blogger.com,1999:blog-3237724005744642470.post1894447505569910163..comments2016-05-20T11:57:55.033+01:00Comments on Captain Debug's Blog: Using Spring’s SimpleMappingExceptionHandlerRoger Hugheshttp://www.blogger.com/profile/07042290171112551665[email protected]Blogger6125tag:blogger.com,1999:blog-3237724005744642470.post-4219306305083769892012-06-28T09:25:16.230+01:002012-06-28T09:25:16.230+01:00Hi Anonymous,
You're right in saying that the ...Hi Anonymous,<br />You're right in saying that the SimpleMappingExceptionResolver is just a view resolver, but the question t ask here is, so far as your code goes, whats the difference between a view resolver and a controller as both aim to return a model and view back to the app for display. <br /><br />In overriding<br /><br />protected ModelAndView doResolveException(HttpServletRequest request, HttpServletResponse response, Object handler,<br /> Exception ex)<br /><br />you have full control of what's going on. Firstly you can return a ModelAndView object containing the name of the view and any data you want to add to it. Secondly, I think that the 'Object handler' argument is a reference to your controller (it certainly is in the complementary Spring HandlerInterceptorAdapter class), so you should theoretically be able to access any of it's properties and create a detailed error page.<br /><br />If I'm wrong here please let me know...Roger Hugheshttp://www.blogger.com/profile/07042290171112551665[email protected]tag:blogger.com,1999:blog-3237724005744642470.post-57431402424324757172012-06-27T22:06:13.610+01:002012-06-27T22:06:13.610+01:00Thanks for the response! I've read through al...Thanks for the response! I've read through all your posts...good stuff! <br /><br />So, the obvious answer would be to use the ExceptionHandler and ResponseStatus annotations. I'm thinking that it might be worth creating a parent controller to house at least the more generic handleExceptions methods. ...might try that out.<br /><br />I could also extend SimpleMappingExceptionResolver and map up statuses to exceptions there. I think the only 2 I plan on returning would be 404 and 500. Either way, returning an error page with status 200 is bad for SEO.<br /><br />Another issue I've found with SimpleMappingExceptionResolver is that it's a view resolver and not a controller. I'm a bit confused by this actually, but I think the significance is that if you have a 404 page that needs data for it's header and footer and usually would get that from a controller it won't get that data because it's controller isn't run at this stage. If I'm correct, then that's a fairly large issue because you can only serve static content unless you do some work to get the data in your SimpleMappingExceptionResolver subclass. That doesn't seem like a responsibility of a view resolver! I've found through trial and error though that the web.xml error-page configs seem to run the full request over again.<br /><br />...long one, but I welcome any of your thoughts on this!<br /><br />tktAnonymous[email protected]tag:blogger.com,1999:blog-3237724005744642470.post-28813149305280496132012-06-02T11:12:46.434+01:002012-06-02T11:12:46.434+01:00tkt,
Also note that using the SimpleMappingExcepti...tkt,<br />Also note that using the SimpleMappingExceptionHandler might not be the best way of achieving your aims as this is Spring's older method of dealing with exceptions. If you read through the blogs I've written you'll see that Spring has other approaches to this problem, which may be more suitable.Roger Hugheshttp://www.blogger.com/profile/07042290171112551665[email protected]tag:blogger.com,1999:blog-3237724005744642470.post-59735783618816861192012-06-02T11:06:44.809+01:002012-06-02T11:06:44.809+01:00I have a whole series of blogs on Spring Exception...I have a whole <a href="/p/blogs-on-exception-handling-with-spring.html" rel="nofollow">series of blogs on Spring Exception handling</a>. I think that you'll find what you're looking for in my blog on the <a href="/2012/02/spring-3-mvc-exception-handlers-and_14.html" rel="nofollow">@ResponseStatus annotation</a>, which allows you to change the response code to whatever value you like.Roger Hugheshttp://www.blogger.com/profile/07042290171112551665[email protected]tag:blogger.com,1999:blog-3237724005744642470.post-59584985368301888302012-06-01T17:43:40.701+01:002012-06-01T17:43:40.701+01:00If I'm correct, this would return the error vi...If I'm correct, this would return the error view but the response status would still be 200. How do you change the status to 500, 404, etc in this situation?tkthttp://www.blogger.com/profile/16629753724913999671[email protected]tag:blogger.com,1999:blog-3237724005744642470.post-73906268155123161392012-06-01T17:42:23.493+01:002012-06-01T17:42:23.493+01:00If I'm correct, this would show an Error view,...If I'm correct, this would show an Error view, but the response status would still be 200. How would you change the status to 500, etc in this setup?tkthttp://www.blogger.com/profile/16629753724913999671[email protected]