如何在Spring启动应用程序中删除白标错误页面?

时间:2016-03-05 17:10:47

标签: java spring jsp

我正在运行一个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?

2 个答案:

答案 0 :(得分:2)

您可以使用

删除错误页面自动配置
exclude = { ErrorMvcAutoConfiguration.class } 
您的@SpringBootApplication注释

中的

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

如果您不使用

@SpringBootApplication您可以通过放入配置类

来实现
@EnableAutoConfiguration( exclude = { ErrorMvcAutoConfiguration.class })

答案 1 :(得分:0)

您可以在pom.xml文件中添加tomcat jasper依赖项

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