为什么spring boot 1.5.3 jar无法识别src/main/resources/META-INF/resources/中的jsp文件

2024-05-10

我使用了spring boot + jsp,我想构建一个可执行的jar,如下这个帖子 http://www.logicbig.com/tutorials/spring-framework/spring-boot/boot-serve-dynamic/指出,只需要把jsp文件放入src/main/resources/META-INF/resources/.

幸运的是,我们对于 Jar 项目还有另一个选择:Servlet 3.0 规范允许在 src/main/resources/META-INF/resources/ 中拥有动态页面

但不幸的是我无法成功访问jsp页面。尝试了很长一段时间后我终于决定改变spring-boot-starter-web 1.5.3.RELEASE to 1.4.2.RELEASE和这个帖子演示一样,这次可以了。

那么为什么spring boot 1.5.3不支持放入jsp文件src/main/resources/META-INF/resources/?


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

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 一样工作?下面是我的做法:

1.copy source code of TomcatEmbeddedServletContainerFactory to your class path enter image description here

2.修改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);
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

为什么spring boot 1.5.3 jar无法识别src/main/resources/META-INF/resources/中的jsp文件 的相关文章

随机推荐