简体   繁体   English

Spring MVC JavaConfig webapp中的“error-page”配置? (没有web.xml)

[英]“error-page” configuration in Spring MVC JavaConfig webapp? (no web.xml)

How would I go about adding "error-page" type configurations to a Spring MVC webapp using Java config? 我如何使用Java配置向Spring MVC webapp添加“错误页面”类型配置? (No web.xml)? (没有web.xml)?

<error-page>
     <error-code>404</error-code>
     <location>/errors/404</location>
</error-page>

I'd like to use a configuration like this (in Java Config) to forward all uncaught Exceptions to a specific controller method. 我想使用这样的配置(在Java Config中)将所有未捕获的异常转发到特定的控制器方法。

I was hoping to avoid the @ControllerAdvice / @ExceptionHandler configuration (which allows me to create a controller method that would handle ALL errors) because I'd like Access Denied exceptions to continue to be caught by Spring Security, and just let any other exceptions get handled by my code. 我希望避免@ControllerAdvice / @ExceptionHandler配置(这允许我创建一个可以处理所有错误的控制器方法),因为我希望Spring Denied继续捕获Access Denied异常,并且只允许任何其他异常由我的代码处理。

It looks like a similar question was asked here: Spring JavaConfig is not catching PageNotFound? 这看起来像是一个类似的问题: Spring JavaConfig没有捕获PageNotFound?

看看https://java.net/jira/browse/SERVLET_SPEC-50 - 没有web.xml就无法配置它,但你可以创建手动过滤器来为你做同样的事情。

You can create SimpleMappingExceptionResolver bean, and set pages for http errors, without using web.xml. 您可以创建SimpleMappingExceptionResolver bean,并为http错误设置页面,而无需使用web.xml。 Or default views for any exceptions. 或任何例外的默认视图。 Here: 这里:

    @Bean
    public SimpleMappingExceptionResolver simpleMappingExceptionResolver(){

    SimpleMappingExceptionResolver resolver = new SimpleMappingExceptionResolver();

    //page and statusCode pare 
    //you also can use method setStatusCodes(properties)
    resolver.addStatusCode(viewName, statusCode); 

    //set views for exception 
    Properties mapping = new Properties();
    mapping.put("ua.package.CustomException" , "page")    
    resolver.setExceptionsMapping(mapping); 

    return resolver;
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM