【问题标题】:Custom error pages in Spring Boot 1.4 not picked up when using JSP resolver使用 JSP 解析器时未拾取 Spring Boot 1.4 中的自定义错误页面
【发布时间】:2016-12-07 10:49:15
【问题描述】:

我正在尝试在我的 Spring Boot 1.4 应用程序中使用我自己的自定义错误页面。根据文档,将我的错误页面放在src/main/resources/public/error 目录中就足够了(例如404.html)。

但是,我也在我的应用程序中使用 JSP 页面,并且有一个解析器:

@Override
public void configureViewResolvers(final ViewResolverRegistry registry) {
    final UrlBasedViewResolverRegistration resolver = registry.jsp("/WEB-INF/jsp/", ".jsp");

    final Map<String, Object> attributes = new HashMap<>();
    attributes.put("HASH", hashReader.getHashValue());
    attributes.put("Hoker", hookerReader.getHooker());
    resolver.attributes(attributes);
}

每当我遇到4xx 错误时,它不会使用我放在resources/public/error 目录中的自定义错误页面,而是尝试加载/WEB-INF/jsp/error.jsp

有没有办法强制 Spring Boot 使用其默认行为,而不是尝试将错误页面解析到 JSP 目录?

【问题讨论】:

  • 这实际上是默认行为...它尝试解析为名为 404 的视图(或任何取决于错误代码的视图)。这将传递给您的视图解析器,该解析器没有(但由于它是基于 Url 的版本总是返回一个 URL)。重定向失败,这反过来会导致尝试解析名为 error 的视图,该视图又会再次传递给您的视图解析器。
  • 但我在 resources/public/error/404.html 中有一个视图名称。但它被忽略了
  • 正确。这就是我解释的...404 是视图名称,它被传递给您的ViewResolver,它试图解析/WEB-INF/jsp/404.jsp 上的视图。默认行为实际上就是这样。通常,当没有注册UrlBasedViewResolver 时,它最终会传递给默认的静态资源处理。但是由于UrlBasedViewResolver 的存在而没有发生。

标签: java jsp spring-boot error-handling


【解决方案1】:

这是一个例子,https://github.com/lenicliu/eg-spring/tree/master/eg-spring-boot/eg-spring-boot-webmvc

我想你可以这样解决它:

package com.lenicliu.spring.boot;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.context.embedded.ConfigurableEmbeddedServletContainer;
import org.springframework.boot.context.embedded.EmbeddedServletContainerCustomizer;
import org.springframework.boot.web.servlet.ErrorPage;
import org.springframework.context.annotation.Bean;
import org.springframework.http.HttpStatus;

@SpringBootApplication
public class Application {

    @Bean
    public EmbeddedServletContainerCustomizer customizeContainerr() {
        return new CustomizedContainer();
    }

    public static void main(String[] args) {
        SpringApplication.run(Application.class, args);
    }

    private static class CustomizedContainer implements EmbeddedServletContainerCustomizer {
        @Override
        public void customize(ConfigurableEmbeddedServletContainer container) {
            container.addErrorPages(new ErrorPage(HttpStatus.NOT_FOUND, "/404.html"));
            container.addErrorPages(new ErrorPage(HttpStatus.INTERNAL_SERVER_ERROR, "/500.html"));
        }
    }
}

您可以将 404.html 和 500.html 放入以下文件夹:

src/main/resource/static/500.html
src/main/resource/static/404.html

像这样:

container.addErrorPages(new ErrorPage(HttpStatus.NOT_FOUND, "/error/404.html"));
container.addErrorPages(new ErrorPage(HttpStatus.INTERNAL_SERVER_ERROR, "/error/500.html"));

然后放入

src/main/resource/static/error/500.html
src/main/resource/static/error/404.html

引用http://docs.spring.io/spring-boot/docs/1.4.2.RELEASE/reference/htmlsingle/#boot-features-spring-mvc-static-content

/static 或 /public 或 /resources 或 /META-INF/resources,它们是相同的。

希望能帮到你:)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-02-11
    • 2017-08-07
    • 2014-11-30
    • 2019-08-14
    • 2014-08-14
    • 2017-10-06
    • 2020-10-24
    • 2013-02-01
    相关资源
    最近更新 更多