zhuguowei
zhuguowei

Reputation: 8487

Why spring boot 1.5.3 jar does not recognise jsp files in src/main/resources/META-INF/resources/

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/.

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/

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.

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

Upvotes: 2

Views: 1853

Answers (2)

user9912218
user9912218

Reputation: 1

Starting with version 1.4.3 for spring-boot-maven-plugin the jsp files are not loaded anymore. To solve the problem use:

<build>
    <plugins>
      <plugin>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-maven-plugin</artifactId>
        <version>1.4.2.RELEASE</version>
        <executions>
          <execution>
            <goals>
              <goal>repackage</goal>
            </goals>
          </execution>
        </executions>
      </plugin>
    </plugins>
  </build>

You can find an example here: https://www.logicbig.com/tutorials/spring-framework/spring-boot/boot-exploded-war.html

Upvotes: 0

zhuguowei
zhuguowei

Reputation: 8487

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());
        }
    }

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

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

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);
}

Upvotes: 1

Related Questions