java - 如何删除 Spring Boot 应用程序中的白色标签错误页面?

标签 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?

最佳答案

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

exclude = { ErrorMvcAutoConfiguration.class } 

在您的@SpringBootApplication注释中

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

如果您没有使用

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

@EnableAutoConfiguration( exclude = { ErrorMvcAutoConfiguration.class })

关于java - 如何删除 Spring Boot 应用程序中的白色标签错误页面?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35817117/

相关文章:

java - 我们需要运行多少次 Java 程序来预热 JVM?

java - maven-shade-plugin 和单例

java - 如何用MongoDB和Spring实现软(逻辑)删除?

java - 如何使 HTML 页面(不是 JSP)与 Servlet 进行通信?

java - "Negative time"尝试运行使用 tomcat7-maven-plugin :exec-war-only 生成的 WAR

java - 与包含冒号的正则表达式匹配字符串

java - 使用包含小数的数字通配符检查文件夹是否存在

spring - 切入点未应用于抽象方法

java - 如何对整数值使用 @Digits 验证

java - 如何从文件系统调用图像?