如何使用java配置和没有Web.xml处理Spring MVC中的404页面未找到异常 [英] How to handle 404 page not found exception in Spring MVC with java configuration and no Web.xml

查看:179
本文介绍了如何使用java配置和没有Web.xml处理Spring MVC中的404页面未找到异常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在我的Spring MVC网络应用程序中处理404页面未找到异常,我正在使用 SPRING 4.2.5.RELEASE ,我已经阅读了几个有关此问题的问题主题,但类似的问题是使用不同的spring java配置。

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.

我有一个全局异常处理程序控制器类,它有我的所有异常,这个类工作正常,但我可以' t处理404页面找不到异常。

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 )我创建了一个名为 ResourceNotFoundException 的类,它扩展自 RuntimeException ,我把这个注释放在类定义上 @ResponseStatus(HttpStatus.NOT_FOUND)

1) I created a class named ResourceNotFoundException that extends from RuntimeException and I putted this annotation over the class definition @ResponseStatus(HttpStatus.NOT_FOUND)

这样:

@ResponseStatus(HttpStatus.NOT_FOUND)
public class ResourceNotFoundException extends RuntimeException { 

}

2)我在异常的控制器cl中创建了这个方法ass

2) I created this method in my exception's controller class

@ExceptionHandler(ResourceNotFoundException.class)
@ResponseStatus(HttpStatus.NOT_FOUND)
public String handleResourceNotFoundException() {

    return "notFoundJSPPage";
}

但是当我放一个不存在的URL时,我收到此错误找不到带URI的HTTP请求的映射

But still when I put a URL that doesn't exist I get this error "No mapping found for HTTP request with URI"

我读过的问题说我需要为Dispatcher启用true选项但是因为我的配置不同从其他问题,我没有 Web.xml 我无法申请。

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.

这里这是我的Config.java

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;
    }

}

这是我的WebInitializer

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{

}


推荐答案

我通过将此行放入我的 onStartup 方法在 WebApplicationInitializer.class

I solved the problem by putting this line in my onStartup method in the WebApplicationInitializer.class

这个这是我添加的行 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");
}

然后我在 GlobalExceptionHandlerController中创建了这个控制器方法。 class

@ExceptionHandler(NoHandlerFoundException.class)
@ResponseStatus(HttpStatus.NOT_FOUND)
public String handle(NoHandlerFoundException ex) {

  return "my404Page";
}

这解决了我的问题我删除了 handleResourceNotFoundException 我的 GlobalExceptionHandlerController.class 中的控制器方法,因为没有必要,我也删除了异常类 ResourceNotFoundException.class

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配置和没有Web.xml处理Spring MVC中的404页面未找到异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆