2016-04-16 39 views
0

无论何时用户在浏览器中输入错误的url,它将导致NoSuchRequestHandlingMethodException我想返回一个自定义视图,而不是Spring提供消息的默认视图。我有@AdviceController其中我试图通过@ExceptionHAndler(NoSuchRequestHandlingMethodException.class)赶上它,并返回一个视图,但它没有奏效。我也继承了DefaultHandlerExceptionResolver,并且提出了处理NoSuchRequestHandlingMethodException的方法,这也没有奏效。我也尝试通过扩展SimpleMappingExceptionResolversetExecptionMapping(在相同的@AdviceController类中)方法注册视图名称以避免异常。我不知道我做错了什么。我什至对春天抛出NoSuchRequestHandlingMethodException当它没有在控制器中找到任何映射有人可以告诉我如何返回与HTTP错误代码相匹配的Spring异常的自定义视图。在Spring MVC中返回NoSuchRequestHandlingMethodException的404错误的自定义视图

这是我@ControllerAdvice

@ControllerAdvice 
public class ExceptionHandlerController extends  
DefaultHandlerExceptionResolver { 

@ExceptionHandler(NoSuchRequestHandlingMethodException.class) 
public String handleException(NoSuchRequestHandlingMethodException.class, Model model) { 

    String error = th.getMessage(); 
    model.addAttribute("error", error); 
    model.addAttribute(new User()); 
    return "login"; 
    } 

@override 
protected ModelAndView handleNoSuchRequestHandlingMethodException 
(NoSuchRequestHandlingMethodException ex, HttpServletRequest request, 
HttpServletResponse response, Object handler) throws Exception { 

     ModelAndView mav = new ModelAndView("notfound"); 
     return mav; 
    } 
} 

UPDATE:解决方案

NoSuchRequestHandlingMethodException没有引起人们的关注,是因为调度Servlet的委托请求默认的servlet处理程序(在我的情况DefaultServletHttpRequestHandler)和认为它处理。我已经实现的解决方法是,我创建了一个映射'/ *'的处理程序方法,当没有在任何控制器中找到特定的映射并且我通过新的NoSuchRequestHandlingMethodException可以在@ExceptionHandler中捕获,然后返回自定义视图。但是,我不知道处理这种异常的缺点。如果有的话让我知道。

@Controller 
public class HomeController { 

... 

@RequestMapping(value = "/*", method = RequestMethod.GET) 
public void handleAtLast(HttpServletRequest request) throws NoSuchRequestHandlingMethodException { 
    throw new NoSuchRequestHandlingMethodException(request); 
    } 
} 

ExceptionHandlerController.java

@ControllerAdvice 
public class ExceptionHandlerController extends DefaultHandlerExceptionResolver { 
    @ExceptionHandler(NoSuchRequestHandlingMethodException.class) 
    public String handleNoSuchMethodtFoundException(NoSuchRequestHandlingMethodException ex){ 
     return "error/notfound"; 
    } 
} 
+0

这也许[文章将帮助你(http://www.javabeat.net/spring-mvc-404-error-page/) – rapasoft

+0

这是一个当用户在URI中输入任何内容(其中需要名称)时抛出自定义异常。我想返回在浏览器中输入的每个错误url的视图,导致NoSuchRequestHandlingMethodException。 –

+0

NoSuchRequestHandlingMethodException被@Deprecated – BigDong

回答

0

尝试这种方式

@ControllerAdvice 
public class AppWideExceptionHandler { 

    @ExceptionHandler(Exception.class) // replace with your exception Class 
    public String errorHandler(){ 
     System.out.println("caught exception"); 
     return "home"; // your view 
    } 
} 
+0

我试过了。它适用于你的自定义异常,但它不适用于Spring本身针对Dispatcher servlet出错的情况 –

0

NoSuchRequestHandlingMethodException没有引起人们的关注,是因为调度Servlet的委托请求默认的servlet处理程序(在我的情况DefaultServletHttpRequestHandler ),并认为它处理。我已经实现的解决方法是,我创建了一个映射'/ *'的处理程序方法,当没有在任何控制器中找到特定的映射并且我通过新的NoSuchRequestHandlingMethodException可以在@ExceptionHandler中捕获,然后返回自定义视图。但是,我不知道处理这种异常的缺点。如果有的话让我知道。

@Controller 
public class HomeController { 

... 

@RequestMapping(value = "/*", method = RequestMethod.GET) 
public void handleAtLast(HttpServletRequest request) throws NoSuchRequestHandlingMethodException { 
    throw new NoSuchRequestHandlingMethodException(request); 
    } 
} 

ExceptionHandlerController.java

@ControllerAdvice 
public class ExceptionHandlerController extends DefaultHandlerExceptionResolver { 
    @ExceptionHandler(NoSuchRequestHandlingMethodException.class) 
    public String handleNoSuchMethodtFoundException(NoSuchRequestHandlingMethodException ex){ 
     return "error/notfound"; 
    } 
}