2

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

zhuguowei
  • 8,401
  • 16
  • 70
  • 106
  • Just to be _clear_, if you follow the post, it tells you to put the views in `src/main/resources/META-INF/resources/*WEB-INF/views*` AND setting `spring.mvc.view.prefix=/WEB-INF/views/` and `spring.mvc.view.suffix=.jsp`. Have you done that? Or you just placed the jsp files in `src/main/resources/META-INF/resources`? – Strelok Jun 22 '17 at 04:24
  • Of course I know these things. You could download the demo and just modify the spring boot version to prove it – zhuguowei Jun 22 '17 at 04:46
  • That is a limitation of using JSP with an embedded container. See https://docs.spring.io/spring-boot/docs/current/reference/html/boot-features-developing-web-applications.html#boot-features-jsp-limitations. What you describe (and link to) is the default when deploying applications, as a war, to an servlet container. – M. Deinum Jun 22 '17 at 06:54
  • For Spring Boot 2 see : https://stackoverflow.com/a/50846032/1634131 Worked for me! – Filip Apr 10 '19 at 15:47

2 Answers2

1

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);
}
zhuguowei
  • 8,401
  • 16
  • 70
  • 106
0

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