spring-boot jsp whitelabel error page问题解决

来源:互联网 发布:mysql数据库默认密码 编辑:程序博客网 时间:2024/05/17 08:16

spring-boot jsp whitelabel error page问题解决

spring-boot配置jsp,访问:http://127.0.0.1:8080,显示如下错误信息:

Whitelabel Error PageThis application has no explicit mapping for /error, so you are seeing this as a fallback.Tue Dec 12 13:04:58 CST 2017There 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未能正确指向项目路径,打断点翻看源代码TomcatEmbeddedServletContainerFactory.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容器源码后,可写个配置TomcatConfig.java解决此问题:

@Configurationpublic 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: xfoobar@163.com

阅读全文
0 0