CodexBloom - Programming Q&A Platform

Spring MVC: scenarios When Trying to Use @ExceptionHandler for Custom handling with ModelAndView

👀 Views: 87 đŸ’Ŧ Answers: 1 📅 Created: 2025-08-07
spring-mvc exception-handling modelandview Java

I'm having a hard time understanding I'm experiencing an scenario with handling custom exceptions in my Spring MVC application. I have a custom exception called `ResourceNotFoundException` that I want to handle globally using an `@ExceptionHandler` method in my controller. However, when this exception is thrown, I receive the following behavior message: `java.lang.IllegalStateException: No view found for name 'errorView'`. I've defined my exception handler like this: ```java @ControllerAdvice public class GlobalExceptionHandler { @ExceptionHandler(ResourceNotFoundException.class) public ModelAndView handleResourceNotFoundException(ResourceNotFoundException ex) { ModelAndView modelAndView = new ModelAndView("errorView"); modelAndView.addObject("message", ex.getMessage()); return modelAndView; } } ``` I've also created a corresponding JSP page named `errorView.jsp` located in `/WEB-INF/views/`. I configured my view resolver like this: ```java @Bean public InternalResourceViewResolver viewResolver() { InternalResourceViewResolver resolver = new InternalResourceViewResolver(); resolver.setPrefix("/WEB-INF/views/"); resolver.setSuffix(".jsp"); return resolver; } ``` Despite this, the application does not seem to locate the `errorView.jsp` when the exception is thrown. I've tried placing debug logs in the handler method, and they confirm that the handler is indeed being triggered. However, I need to figure out why the view is not being resolved properly. Any insights on what might be going wrong or how to troubleshoot this would be greatly appreciated. I'm using Spring MVC version 5.3.10 and JSP 2.3. I'm working on a service that needs to handle this. I appreciate any insights!