|
|
SimpleMappingExceptionResolver a quick question
Hi all!
I have a rather complex application running, with a lot of controllers etc. Unfortunately the system was originally build without any exception handling. I am now trying to get things to work, and so far everything is ok - except one thing.
I have defined:Code:
lt;!-- Exeption resolver --gt;
lt;bean id=quot;exceptionResolverquot; class=quot;org..web.servlet.handler.SimpleMappingExceptionResolverquot;gt; lt;property name=quot;defaultErrorViewquot; value=quot;errorViewquot; /gt;
lt;/beangt;
In my xxx-servlet.xml file to map all exceptions to the defined view. I of course have a jsp page to fit this view name - errorView.jsp.
This is what works fine. If I provoke an exception, its caught and this view is shown.
What I would like to do now is add a controller and a formobject to this view. However, dooing it in the normal way by adding the logic view name to:Code:
lt;bean id=quot;uclMappingquot; class=quot;org..web.servlet.handler.SimpleuclHandlerMappingquot;gt; lt;property name=quot;mappingsquot;gt; lt;propsgt;.....lt;prop key=quot;/errorView.htmquot;gt;errorViewControllerlt;/propgt; lt;/propsgt; lt;/propertygt;
lt;/beangt;
With a corresponding bean:Code:
lt;bean id=quot;errorViewControllerquot; class=quot;com.myapp.web.controller.ErrorControllerquot;gt; lt;property name=quot;commandNamequot; value=quot;errorFormquot;/gt; lt;property name=quot;formViewquot; value=quot;errorViewquot;/gt; lt;property name=quot;successViewquot; value=quot;errorViewquot;/gt;
lt;/beangt;
Doesn't really do the trick - at least not with the above configuration. I would like to do it in a somewhat similarly manner, because I do not want to have to edit 300+ controllers.
Is this possible? And if so, what do I need more?
I have noticed, when the exception is thrown and the errorView is shown, the ucl is not updated to this, so my guess is, that it happens internally, which is why the SimpleuclHandlerMapping doesn't catch it...
Any comments or suggestions appreciated!
BR
Hovendal
The SimpleMappingExceptionResolver basically works as a controller. It delivers a view name which is passed to the ViewResolver. If you want to invoke a controller you will have to use a redirect with a ucl to a controller.
So I need to do a from redirect from my errorView to a new page or what do you mean by a redirect?
That seems kinda ugly
Well if you want to invoke a controller... Simply adding a redirect: and extension to your error view should be enough.Code:
lt;property name=quot;defaultErrorViewquot; value=quot;redirect:errorView.htmquot; /gt;That sounded almost to great to be true - and unfortunately it was :/
Changing:
Code:
lt;bean id=quot;exceptionResolverquot; class=quot;org..web.servlet.handler.SimpleMappingExceptionResolverquot;gt; lt;property name=quot;defaultErrorViewquot; value=quot;errorViewquot; /gt;
lt;/beangt;
To:Code:
lt;bean id=quot;exceptionResolverquot; class=quot;org..web.servlet.handler.SimpleMappingExceptionResolverquot;gt; lt;property name=quot;defaultErrorViewquot; value=quot;redirect:errorView.htmquot; /gt;
lt;/beangt;
Causes the exception to be written directly to the screen - e.g the exception was not caught by properly...
Are you sure this is it?
If I use the ucl directly the view is rendered correctly, so it is not the controller/view that is erroneous...
You have to make sure that the redirect contains the ucl to redirect to, if you don't you might get other strange issues. So probably errorView.htm is a bit to short.
Which version of Spring and which app server are you using... There is a known issue with tomcat and a certain spring version.
Well, it seems this is a known issue in 2.5.5
browse/SPR-5042
Trying 2.5.6 to see if this solves it. I think I need to put a quot;/quot; in front of the view name. so redirect:/errorView.htm should properly be right?
Originally Posted by HovendalWell, it seems this is a known issue in 2.5.5
That is what I hinted at .
If that is the full path inside your servlet mapping then that should be enough.
Thank you!
That worked exactly as it should! Upgrading to 2.5.6 did the trick!
However, the exception is by default placed in the model under the name quot;exceptionquot;, but this is no longer accessible. Can I pass the thrown exception on to the controller in some way?
Before I used to just print it out with a small scriptlet:
Code:
lt;%
Exception e = (Exception)pageContext.getAttribute(quot;exceptionquot;, PageContext.REQUEST_SCOPE);
e.printStackTrace(new PrintWriter(out));
%gt;
But this is now null...
Hmm that is a bit problematic, because well it is a redirect and thus a new request. The easiest approach is to extend the SimpleMappingExceptionResolver and override the doResolveException method, first call super and then store the exception in the fromsession. That way it it available in the controller, which can then retrieve it and use it.
Regarding your code, I wouldn't recommend scriptlets in jsp pages, they are quite cumbersome to maintain in the long run. I suggest use jstl to print out the information. |
|