java - 如何在带有java配置且没有Web.xml的Spring MVC中处理404页面未找到异常 [英] How to handle 404 page not found exception in Spring MVC with java configuration and no Web.xml
问题描述
我想在我的 Spring MVC web 应用程序中处理 404 页面未找到异常,我正在使用 SPRING 4.2.5.RELEASE
,我已经阅读了几个关于这个主题的问题,但类似的问题是使用不同的 spring java 配置.
我有一个全局异常处理程序控制器类,其中包含我所有的异常,这个类工作正常,但我无法处理 404 页面未找到异常.
这是我按照教程采取的方法
1) 我创建了一个名为 ResourceNotFoundException
的类,它从 RuntimeException
扩展而来,我把这个注解放在了类定义 @ResponseStatus(HttpStatus.NOT_FOUND)
代码>
像这样:
@ResponseStatus(HttpStatus.NOT_FOUND)公共类 ResourceNotFoundException 扩展 RuntimeException {}
2) 我在异常的控制器类中创建了这个方法
@ExceptionHandler(ResourceNotFoundException.class)@ResponseStatus(HttpStatus.NOT_FOUND)公共字符串 handleResourceNotFoundException() {返回notFoundJSPPage";}
但是当我输入一个不存在的 URL 时,我仍然收到此错误未找到带有 URI 的 HTTP 请求的映射"
我读过的问题说我需要为 Dispatcher 启用一个选项,但由于我的配置与其他问题不同,而且我没有 Web.xml
我不能应用.
这是我的 Config.java
@EnableWebMvc@配置@ComponentScan({"config", "controllers"})公共类 ConfigMVC 扩展了 WebMvcConfigurerAdapter {@覆盖public void addResourceHandlers(ResourceHandlerRegistry registry) {registry.addResourceHandler("/resources/**").addResourceLocations("/WEB-INF/resources/");}@豆角,扁豆公共 UrlBasedViewResolver setupViewResolver() {UrlBasedViewResolver 解析器 = new UrlBasedViewResolver();resolver.setPrefix("/WEB-INF/jsp/");resolver.setSuffix(".jsp");resolver.setViewClass(JstlView.class);返回解析器;}}
这是我的 WebInitializer
public class WebInicializar 实现了 WebApplicationInitializer {@覆盖public void onStartup(ServletContext servletContext) 抛出 ServletException {AnnotationConfigWebApplicationContext ctx = new AnnotationConfigWebApplicationContext();ctx.register(ConfigMVC.class);ctx.setServletContext(servletContext);动态 servlet = servletContext.addServlet("dispatcher", new DispatcherServlet(ctx));servlet.addMapping("/");servlet.setLoadOnStartup(1);}}
这是我的全局异常处理程序控制器
@ControllerAdvice公共类 GlobalExceptionHandlerController {@ExceptionHandler(value = NullPointerException.class)public String handleNullPointerException(Exception e) {System.out.println("发生空指针异常" + e);返回空指针异常页面";}@ResponseStatus(value = HttpStatus.INTERNAL_SERVER_ERROR)@ExceptionHandler(value = Exception.class)public String handleAllException(Exception e) {System.out.println("发生未知异常:" + e);返回unknownExceptionPage";}@ExceptionHandler(ResourceNotFoundException.class)@ResponseStatus(HttpStatus.NOT_FOUND)公共字符串 handleResourceNotFoundException() {返回notFoundJSPPage";}}
以及我创建的扩展运行时异常的类
@ResponseStatus(HttpStatus.NOT_FOUND)公共类 ResourceNotFoundException 扩展了 RuntimeException{}
我通过将这一行放在 WebApplicationInitializer.class
中的 onStartup
方法中解决了这个问题>
这是我添加的行 servlet.setInitParameter("throwExceptionIfNoHandlerFound", "true");
这是我添加的新行的完整方法的外观
@Overridepublic void onStartup(ServletContext servletContext) 抛出 ServletException {AnnotationConfigWebApplicationContext ctx = new AnnotationConfigWebApplicationContext();ctx.register(ConfigMVC.class);ctx.setServletContext(servletContext);动态 servlet = servletContext.addServlet("dispatcher", new DispatcherServlet(ctx));servlet.addMapping("/");servlet.setLoadOnStartup(1);servlet.setInitParameter("throwExceptionIfNoHandlerFound", "true");}
然后我在我的 GlobalExceptionHandlerController.class
@ExceptionHandler(NoHandlerFoundException.class)@ResponseStatus(HttpStatus.NOT_FOUND)公共字符串句柄(NoHandlerFoundException ex){返回my404Page";}
这解决了我的问题,我删除了 GlobalExceptionHandlerController.class
中的 handleResourceNotFoundException
控制器方法,因为它不是必需的,而且我还删除了异常类 ResourceNotFoundException我创建的 .class
I want to handle 404 page not found exception in my Spring MVC web app, I'm using SPRING 4.2.5.RELEASE
, I had read several question regarding this topic but the similar questions are using a different spring java configuration.
I have a Global Exception Handler Controller class that have all my Exceptions, this class works fine but I can't handle a 404 page not found exception.
This is the approach that I take following a tutorial
1) I created a class named ResourceNotFoundException
that extends from RuntimeException
and I putted this annotation over the class definition @ResponseStatus(HttpStatus.NOT_FOUND)
like this:
@ResponseStatus(HttpStatus.NOT_FOUND)
public class ResourceNotFoundException extends RuntimeException {
}
2) I created this method in my exception's controller class
@ExceptionHandler(ResourceNotFoundException.class)
@ResponseStatus(HttpStatus.NOT_FOUND)
public String handleResourceNotFoundException() {
return "notFoundJSPPage";
}
But still when I put a URL that doesn't exist I get this error "No mapping found for HTTP request with URI"
The questions that I had read said that I need to enable to true an option for the Dispatcher but since my configuration it's different from the other questions and I don't have a Web.xml
I couldn't apply that.
Here it's my Config.java
@EnableWebMvc
@Configuration
@ComponentScan({"config", "controllers"})
public class ConfigMVC extends WebMvcConfigurerAdapter {
@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
registry.addResourceHandler("/resources/**").addResourceLocations("/WEB-INF/resources/");
}
@Bean
public UrlBasedViewResolver setupViewResolver() {
UrlBasedViewResolver resolver = new UrlBasedViewResolver();
resolver.setPrefix("/WEB-INF/jsp/");
resolver.setSuffix(".jsp");
resolver.setViewClass(JstlView.class);
return resolver;
}
}
Here is my WebInitializer
public class WebInicializar implements WebApplicationInitializer {
@Override
public void onStartup(ServletContext servletContext) throws ServletException {
AnnotationConfigWebApplicationContext ctx = new AnnotationConfigWebApplicationContext();
ctx.register(ConfigMVC.class);
ctx.setServletContext(servletContext);
Dynamic servlet = servletContext.addServlet("dispatcher", new DispatcherServlet(ctx));
servlet.addMapping("/");
servlet.setLoadOnStartup(1);
}
}
Here is my Global Exception Handler Controller
@ControllerAdvice
public class GlobalExceptionHandlerController {
@ExceptionHandler(value = NullPointerException.class)
public String handleNullPointerException(Exception e) {
System.out.println("A null pointer exception ocurred " + e);
return "nullpointerExceptionPage";
}
@ResponseStatus(value = HttpStatus.INTERNAL_SERVER_ERROR)
@ExceptionHandler(value = Exception.class)
public String handleAllException(Exception e) {
System.out.println("A unknow Exception Ocurred: " + e);
return "unknowExceptionPage";
}
@ExceptionHandler(ResourceNotFoundException.class)
@ResponseStatus(HttpStatus.NOT_FOUND)
public String handleResourceNotFoundException() {
return "notFoundJSPPage";
}
}
And the class I created that extends Runtime Exception
@ResponseStatus(HttpStatus.NOT_FOUND)
public class ResourceNotFoundException extends RuntimeException{
}
I solved the problem by putting this line in my onStartup
method in the WebApplicationInitializer.class
this it's the line I add servlet.setInitParameter("throwExceptionIfNoHandlerFound", "true");
this is how it looks the complete method with the new line I added
@Override
public void onStartup(ServletContext servletContext) throws ServletException {
AnnotationConfigWebApplicationContext ctx = new AnnotationConfigWebApplicationContext();
ctx.register(ConfigMVC.class);
ctx.setServletContext(servletContext);
Dynamic servlet = servletContext.addServlet("dispatcher", new DispatcherServlet(ctx));
servlet.addMapping("/");
servlet.setLoadOnStartup(1);
servlet.setInitParameter("throwExceptionIfNoHandlerFound", "true");
}
Then I created this controller method in my GlobalExceptionHandlerController.class
@ExceptionHandler(NoHandlerFoundException.class)
@ResponseStatus(HttpStatus.NOT_FOUND)
public String handle(NoHandlerFoundException ex) {
return "my404Page";
}
and that solved my problem I deleted the handleResourceNotFoundException
controller method in my GlobalExceptionHandlerController.class
since it wasn't necessary and also I deleted the exception class ResourceNotFoundException.class
that I created
这篇关于java - 如何在带有java配置且没有Web.xml的Spring MVC中处理404页面未找到异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!