Results 1 to 5 of 5

Thread: Showing an error occurred view on exception or 404

  1. #1
    Join Date
    Mar 2012
    Posts
    25

    Default Showing an error occurred view on exception or 404

    which works fine for all exeptions occuring under core url.
    I have

    Code:
    @Controller(value="core/*")
    public class CoreController {
    
        public static String exceptionOccurredView = "/core/exceptionOccurred";
    
        @ExceptionHandler(Throwable.class)
        public ModelAndView exceptionOccurred(Throwable exception, HttpServletResponse response, HttpServletRequest request) {
            ModelAndView mv = new ModelAndView();
            mv.setViewName ( exceptionOccurredView );
            mv.addObject   ( "requestedUrl", Core.getCurrentUrlWithParams() );
            mv.addObject   ( "exception", exception );
    
            System.out.println( "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" + Core.getCurrentUrlWithParams() );
            return mv;
        }   
    
        @RequestMapping
        public void test0(HttpServletResponse response, HttpServletRequest request) throws IOException {
            throw new IOException();
        } 
    
    }

    if I go to url

    localhost:8080/core/test0

    the error page is shown. Also if I go to:

    localhost:8080/core/actionDoesNotExist

    but if I use:

    localhost:8080/controllerDoesNotExist/test0

    The error does not get shown, since the annotation @ExceptionHandler is valid only per controller.

    So how can you achieve a global, non-controller attached exception/error handler ?

  2. #2
    Join Date
    Jun 2006
    Location
    The Netherlands
    Posts
    13,632

    Default

    By configuring a SimpleMappingHandlerExceptionResolver... You cannot get this behavior with @ExceptionHandler annotated methods.
    Marten Deinum
    Java Consultant / Pragmatist / Open Source Enthousiast / Author


    Pro Spring MVC: With Web Flow
    Conspect

    Have you read the reference guide.
    Use the [ code ] tags, young padawan

  3. #3
    Join Date
    Mar 2012
    Posts
    25

    Default

    That doesnt work either. Exact same behaviour when going for a controller that does not exist. It only works for non-existing action in a controller that does exist.

    I also tried with implementing my own handler. That didn't work either with same result.

  4. #4
    Join Date
    Jun 2006
    Location
    The Netherlands
    Posts
    13,632

    Default

    And that is a url that maps to the dispatcherservlet? It will only work if at least the DispatcherServlet is hit else you have to rely on the exception handling/error pages in the web.xml.

    Doh.. I always fall into this trap... When the handler isn't found the dispatcherservlet handles the exception it sends a 404 to the client. So you have to specify the error-page in the web.xml or override the noHandlerFound method in the DispatcherServlet and create your own implementation.
    Last edited by Marten Deinum; Apr 4th, 2012 at 09:20 AM.
    Marten Deinum
    Java Consultant / Pragmatist / Open Source Enthousiast / Author


    Pro Spring MVC: With Web Flow
    Conspect

    Have you read the reference guide.
    Use the [ code ] tags, young padawan

  5. #5
    Join Date
    Mar 2012
    Posts
    25

    Default

    Yes, I believe a 404 is the way to go... i believe it is only the 404 that requires this type of special handling...

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •