为什么Spring Boot 1.5.3 jar无法识别src/main/resources/META-INF/resources/中的jsp文件 [英] Why spring boot 1.5.3 jar does not recognise jsp files in src/main/resources/META-INF/resources/

查看:417
本文介绍了为什么Spring Boot 1.5.3 jar无法识别src/main/resources/META-INF/resources/中的jsp文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用了spring boot + jsp,我想构建一个可执行jar,如

I used spring boot + jsp, and I want to build a executable jar, as this post pointed out, just need put jsp files into src/main/resources/META-INF/resources/.

幸运的是,我们为Jar项目提供了另一个选择:Servlet 3.0规范允许在src/main/resources/META-INF/resources/中提供动态页面

Fortunately we have another option for a Jar project: Servlet 3.0 specification allows to have dynamic pages in src/main/resources/META-INF/resources/

但是不幸的是,我无法成功访问jsp页面.经过很长时间的尝试,最终我决定将spring-boot-starter-web 1.5.3.RELEASE更改为1.4.2.RELEASE,与本次演示相同,这一次它可以正常工作.

But unfortunately I cannot access jsp page successfully. After trying every manner for a long time finally I decided to change spring-boot-starter-web 1.5.3.RELEASE to 1.4.2.RELEASE as same as this post demo, this time it works.

那么为什么sprint boot 1.5.3不支持在src/main/resources/META-INF/resources/中放置jsp文件?

So why sprint boot 1.5.3 does not support put jsp files in src/main/resources/META-INF/resources/?

推荐答案

跟踪源代码后,我发现为什么1.5.3无法识别jsp文件.

After tracing source code I find why 1.5.3 cannot recognise jsp files.

Spring boot 1.4.2

//org.springframework.boot.context.embedded.tomcat.TomcatEmbeddedServletContainerFactory.StoreMergedWebXmlListener#onStart
private void onStart(Context context) {
    ServletContext servletContext = context.getServletContext();
    if(servletContext.getAttribute("org.apache.tomcat.util.scan.MergedWebXml") == null) {
        servletContext.setAttribute("org.apache.tomcat.util.scan.MergedWebXml", this.getEmptyWebXml());
    }

    TomcatResources.get(context).addClasspathResources(); // only 1.4.2 has this line 
}

Spring boot 1.5.3

    private void onStart(Context context) {
        ServletContext servletContext = context.getServletContext();
        if (servletContext.getAttribute(MERGED_WEB_XML) == null) {
            servletContext.setAttribute(MERGED_WEB_XML, getEmptyWebXml());
        }
    }

如何让Spring Boot 1.5.3也像1.4.2一样工作?以下是我的方式:

And how to let spring boot 1.5.3 also work as 1.4.2? Below is my manner:

1.将TomcatEmbeddedServletContainerFactory的源代码复制到您的类路径中

1.copy source code of TomcatEmbeddedServletContainerFactory to your class path

2.modify onStart方法

2.modify onStart method

private void onStart(Context context) {
    ServletContext servletContext = context.getServletContext();
    if (servletContext.getAttribute(MERGED_WEB_XML) == null) {
        servletContext.setAttribute(MERGED_WEB_XML, getEmptyWebXml());
    }
    // add below code    
    List<URL> list = new ArrayList<>();
    String file = "file:/Users/zhugw/workspace/boot-jar-serving-jsp/boot-jar-serving-jsp-1.0-SNAPSHOT.jar!/";
    try {
        URL jar = new URL("jar", null, file);
        list.add(jar);
    } catch (MalformedURLException e) {
        e.printStackTrace();
    }
    TomcatResources.get(context).addResourceJars(list);
}

这篇关于为什么Spring Boot 1.5.3 jar无法识别src/main/resources/META-INF/resources/中的jsp文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆