开发者

I can't get SimpleMappingExceptionResolver work the way I want

开发者 https://www.devze.com 2023-02-16 00:43 出处:网络
I am using spring 3 and I tried to add exception mapping this way: <bean class=\"org.springframework.web.servlet.handler.SimpleMappingExceptionResolver\">

I am using spring 3 and I tried to add exception mapping this way:

<bean
        class="org.springframework.web.servlet.handler.SimpleMappingExceptionResolver">
        <property name="exceptionMappings">
            <map>
                <entry key="it.jsoftware.jacciseweb.controllers.AcciseAuthException" value="/jsp/errorUserPage.jsp" />              
            </map>
        </property>     
    </bean>

of course jsp(s) are NOT in WEB-INF but in a folder in the root called jsp. What I get is my custom page for 404 handling, that I configured in web.xml. What's wrong?

Here is Spring log with DEBUG enabled:

[08 mar 2011 15:20:11,451 CET] [DEBUG] [HandlerMethodInvoker] [invokeHandlerMethod:173] - Invoking request handler method: public org.springframework.web.servlet.ModelAndView it.jsoftware.jacciseweb.controllers.MainController.prodottiList(int,java.lang.String,boolean,java.lang.Integer,javax.servlet.http.HttpSession) throws java.lang.Exception
[08 mar 2011 15:20:11,452 CET] [DEBUG] [SimpleMappingExceptionResolver] [resolveException:132] - Resolving exception from handler [it.jsoftware.jacciseweb.controllers.MainController@1876a6d]: it.jsoftware.jacciseweb.controllers.AcciseAuthException
[08 mar 2011 15:20:11,454 CET] [DEBUG] [SimpleMappingExceptionResolver] [findMatchingViewName:202] - Resolving to view '/jsp/errorUserPage.jsp' for exception of type [it.jsoftware.jacciseweb.controllers.AcciseAuthException], based on exception mapping [it.jsoftware.jacciseweb.controllers.AcciseAuthException]
[08 mar 2011 15:20:11,454 CET] [DEBUG] [SimpleMappingExceptionResolver] [getModelAndView:294] - Exposing Exception as model attribute 'exception'
[08 mar 2011 15:20:11,458 CET] [DEBUG] [DispatcherServlet] [processHandlerException:1001] - Handler execution resulted in exception - forwarding to resolved error view: ModelAndView: reference to view with name '/jsp/errorUserPage.jsp'; model is {exception=it.jsoftware.jacciseweb.controllers.AcciseAuthException}
it.jsoftware.jacciseweb.controllers.AcciseAuthException
    at it.jsoftware.jacciseweb.controllers.ApplicationUserStatus.checkIfUserPrivilegeBelowOrEqual(ApplicationUserStatus.java:162)
    at it.jsoftware.jacciseweb.controllers.MainController.prodottiList(MainController.java:401)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
    at java.lang.reflect.Method.invoke(Method.java:597)
    at org.springframework.web.bind.annotation.support.HandlerMethodInvoker.invokeHandlerMethod(HandlerMethodInvoker.java:176)
    at org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter.invokeHandlerMethod(AnnotationMethodHandlerAdapter.java:426)
    at org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter.handle(AnnotationMethodHandlerAdapter.java:414)
    at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:790)
    at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:719)
    at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:644)
    at org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:549)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:617)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
    at org.springframework.web.filter.CharacterEncodingFilter.doFilterInternal(CharacterEncodingFilter.java:88)
    at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:76)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:235)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:233)
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:191)
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:127)
    at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102)
    at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:298)
    at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:859)
    at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:588)
    at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:489)
    at java.lang.Thread.run(Thread.java:619)
[08 mar 2011 15:20:11,464 CET] [DEBUG] [DefaultListableBeanFactory] [invokeInitMethods:1461] - Invoking afterPropertiesSet() on bean with nam开发者_C百科e '/jsp/errorUserPage.jsp'
[08 mar 2011 15:20:11,465 CET] [DEBUG] [DefaultListableBeanFactory] [doGetBean:242] - Returning cached instance of singleton bean 'org.springframework.transaction.config.internalTransactionAdvisor'
[08 mar 2011 15:20:11,465 CET] [DEBUG] [DispatcherServlet] [render:1045] - Rendering view [org.springframework.web.servlet.view.JstlView: name '/jsp/errorUserPage.jsp'; URL [/jsp/errorUserPage.jsp]] in DispatcherServlet with name 'springDispatcher'
[08 mar 2011 15:20:11,466 CET] [DEBUG] [JstlView] [exposeModelAsRequestAttributes:328] - Added model object 'exception' of type [it.jsoftware.jacciseweb.controllers.AcciseAuthException] to request in view with name '/jsp/errorUserPage.jsp'
[08 mar 2011 15:20:11,466 CET] [DEBUG] [JstlView] [renderMergedOutputModel:236] - Forwarding to resource [/jsp/errorUserPage.jsp] in InternalResourceView '/jsp/errorUserPage.jsp'
[08 mar 2011 15:20:11,467 CET] [DEBUG] [OpenSessionInViewInterceptor] [afterCompletion:212] - Closing single Hibernate Session in OpenSessionInViewInterceptor
[08 mar 2011 15:20:11,468 CET] [DEBUG] [SessionFactoryUtils] [closeSession:789] - Closing Hibernate Session
[08 mar 2011 15:20:11,468 CET] [DEBUG] [DispatcherServlet] [processRequest:674] - Successfully completed request

UPDATE

I'm adding this for who'll see this question in the future. This configuration is OK, the JSP was just wrong. Thanks anyway to the answer that proposes a better method to handle this is Spring 3.


Did you mean to have .jsp in the exception value? How are your controllers and view resolvers etc set up? Do they append a .jsp to the value before resolving paths etc?

Do you need to have the exception mapping in xml? Could the @ExceptionHandler annotation fullfill your expectations?

Here is a nice example of xml based spring exception resolvers (although spring 2.5 based): http://developingdeveloper.wordpress.com/2008/03/09/handling-exceptions-in-spring-mvc-part-2/

Do note if you go the annotation way that the annotations methods needs to be within the same controller class (or abstract class). I wrote a small example of this: http://blog.flurdy.com/2010/07/spring-mvc-exceptionhandler-needs-to-be.html

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号