spring-boot jsp whiteabel error page問題解決

5615 ワード

spring-boot jsp whiteabel error page問題解決
spring-bootはjspを配置して、訪問:http://127.0.0.1:8080以下のエラーメッセージを表示します。
Whitelabel Error Page

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

Tue Dec 12 13:04:58 CST 2017
There was an unexpected error (type=Not Found, status=404).
/WEB-INF/jsp/test1.jsp
Tomcatコンテキストを印刷してディレクトリを指します。
public static void main(Stringp[] args){
    ConfigurableApplicationContext context = SpringApplication.run(ApplicationMain.class, args);
    ServletContext context1 = context.getBean(ServletContext.class);
    URL url = context1.getResource("/");
    System.out.prinltn("ContextRoot Path: {}", url.getFile());
}

Output:
/C:/Users/ADMINI~1/AppData/Local/Temp/tomcat-docbase.2813863602259672077.8081/
Tomcatがプロジェクトのパスを正確に指すことができなかったと説明しています。ブレークポイントを開いてソースコードTomcatEmbodServletContiner Factory.javaを見ます。
protected void prepareContext(Host host, ServletContextInitializer[] initializers) {
        //        ,         ,
        //    :src/main/webapp/;public;static
        //             ,        。
        File docBase = getValidDocumentRoot();

        docBase = (docBase != null ? docBase : createTempDir("tomcat-docbase"));
        final TomcatEmbeddedContext context = new TomcatEmbeddedContext();
        context.setName(getContextPath());
        context.setDisplayName(getDisplayName());
        context.setPath(getContextPath());
        context.setDocBase(docBase.getAbsolutePath());
        context.addLifecycleListener(new FixContextListener());
        context.setParentClassLoader(
                this.resourceLoader != null ? this.resourceLoader.getClassLoader()
                        : ClassUtils.getDefaultClassLoader());
        resetDefaultLocaleMapping(context);
        addLocaleMappings(context);
        try {
            context.setUseRelativeRedirects(false);
        }
        catch (NoSuchMethodError ex) {
            // Tomcat is < 8.0.30. Continue
        }
        SkipPatternJarScanner.apply(context, this.tldSkipPatterns);
        WebappLoader loader = new WebappLoader(context.getParentClassLoader());
        loader.setLoaderClass(TomcatEmbeddedWebappClassLoader.class.getName());
        loader.setDelegate(true);
        context.setLoader(loader);
        if (isRegisterDefaultServlet()) {
            addDefaultServlet(context);
        }
        if (shouldRegisterJspServlet()) {
            addJspServlet(context);
            addJasperInitializer(context);
            context.addLifecycleListener(new StoreMergedWebXmlListener());
        }
        context.addLifecycleListener(new LifecycleListener() {

            @Override
            public void lifecycleEvent(LifecycleEvent event) {
                if (event.getType().equals(Lifecycle.CONFIGURE_START_EVENT)) {
                    TomcatResources.get(context)
                            .addResourceJars(getUrlsOfJarsWithMetaInfResources());
                }
            }

        });
        ServletContextInitializer[] initializersToUse = mergeInitializers(initializers);
        configureContext(context, initializersToUse);
        host.addChild(context);
        postProcessContext(context);
    }
階層の分析spring-bootにはTomcat容器のソースコードが埋め込まれています。Tomcat Config.javaの構成を書いてこの問題を解決します。
@Configuration
public class TomcatConfig {

    @Bean
    public EmbeddedServletContainerFactory embeddedServletContainerFactory() {
        ConfigurableEmbeddedServletContainer factory = new TomcatEmbeddedServletContainerFactory();
        //        ,      
        factory.setDocumentRoot(new File("D:\\Workspace\\IDEA\\spring-boot-web-demo\\src\\main\\webapp\\"));
        return (EmbeddedServletContainerFactory) factory;
    }
}
Good Luck!:
Contact Mail:[email protected]