【问题标题】:How to remove white label error page in spring boot application?如何在 Spring Boot 应用程序中删除白标错误页面?
【发布时间】:2016-06-19 10:21:45
【问题描述】:

我正在运行一个 Spring Boot 应用程序。当我输入 URL http://localhost:8080(或http://localhost:8080/index.jsp)时,我希望加载 index.jsp 文件,但在浏览器上出现以下错误。

Whitelabel Error Page

    This application has no explicit mapping for /error, so you are seeing this as a fallback.

    Sat Mar 05 21:56:33 IST 2016
    There was an unexpected error (type=Not Found, status=404).
    No message available

我的 index.jsp 存在于 webContent 目录中,我的 AppConfig 类如下

@EnableJpaRepositories("com.test.repository")
@Configuration
@EnableTransactionManagement
@ComponentScan(basePackages="com.test.domain")
@PropertySource(value={"classpath:application.properties"})
public class AppConfig {
    @Autowired
    private Environment environment;

    @Bean
    public DataSource dataSource() {
        DriverManagerDataSource datasource = new DriverManagerDataSource();
        datasource.setDriverClassName(environment.getRequiredProperty("spring.datasource.driver-class-name"));
        datasource.setUrl(environment.getRequiredProperty("spring.datasource.url"));
        datasource.setUsername(environment.getRequiredProperty("spring.datasource.username"));
        datasource.setPassword(environment.getRequiredProperty("spring.datasource.password"));
        return datasource;
    }

    @Bean
    public ViewResolver getViewResolver() {
        InternalResourceViewResolver resolver = new InternalResourceViewResolver();
        resolver.setPrefix("/WEB-INF/jsp/");
        resolver.setSuffix(".jsp");
        return resolver;
    }

    @Bean
    public WebMvcConfigurerAdapter forwarderToIndex() {
        return new WebMvcConfigurerAdapter() {
            @Override
            public void addViewControllers(ViewControllerRegistry registry) {
                registry.addViewController("/").setViewName("forward://index.jsp");
            }
        };
    }

}

我还提到了this,这对我没有帮助。如何消除此错误并重定向到 index.jsp?

【问题讨论】:

  • 请发布您的控制器的代码

标签: java spring jsp


【解决方案1】:

您可以使用删除错误页面自动配置

exclude = { ErrorMvcAutoConfiguration.class } 

在您的 @SpringBootApplication 注释中

 @SpringBootApplication(scanBasePackages = { "com.myapp.app" }, exclude = { ErrorMvcAutoConfiguration.class })

如果你不使用

@SpringBootApplication 你可以通过放置在你的配置类中来做到这一点

@EnableAutoConfiguration( exclude = { ErrorMvcAutoConfiguration.class })

【讨论】:

  • 谢谢。它帮助我消除了白标错误,但我无法加载项目根目录中存在的 index.jsp 文件。
  • @Surajhk 它应该在您的 webapp 文件夹中,并确保您没有覆盖控制器中的路径“/”
  • 它在 webapp 文件夹中,并且没有资源映射为“/”的控制器。当我点击 localhost:8080 时,我仍然得到 404
【解决方案2】:

你可以在你的 pom.xml 文件中添加 tomcat jasper 依赖

<dependency>
    <groupId> org.apache.tomcat </groupId>
    <artifactId> tomcat-jasper </artifactId>
    <version>9.0.5</version>
</dependency>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-08-04
    • 2020-06-22
    • 1970-01-01
    • 2023-01-29
    • 2021-02-20
    • 2019-06-29
    • 2013-06-25
    • 2017-09-18
    相关资源
    最近更新 更多