From b088f60082366e04f28b37d30ee23d5bae44ed25 Mon Sep 17 00:00:00 2001 From: Dave Syer Date: Tue, 18 Feb 2014 12:14:25 +0000 Subject: [PATCH] Investigated JSP support Summary: Tomcat works with WARs. Jetty doesn't work. See gh-367 --- spring-boot/README.md | 35 ++++++++----------- ...stractEmbeddedServletContainerFactory.java | 8 +++-- .../JettyEmbeddedServletContainerFactory.java | 7 +++- 3 files changed, 26 insertions(+), 24 deletions(-) diff --git a/spring-boot/README.md b/spring-boot/README.md index 103e15ab82..eaebeeb2de 100644 --- a/spring-boot/README.md +++ b/spring-boot/README.md @@ -249,29 +249,22 @@ can also register items directly if your bean implements the `ServletContextInit interface. -### JSP limitations -When running a Spring Boot application that uses an embedded servlet container and is -packaged as an executable JAR or WAR, there are some limitations in the JSP support. +### JSP limitations When running a Spring Boot application that uses +an embedded servlet container and is packaged as an executable +archive, there are some limitations in the JSP support. -Due to the way that Jasper (Tomcat and Jetty's JSP engine) loads tag libraries, tag -libraries packaged in JAR files nested within the JAR or WAR will not work correctly. -When packaged and run as an executable JAR, you will see an error message produced during -application startup that is similar to the following: +* With Tomcat it should work if you use WAR packaging. I.e. an + executable WAR will work, and will also be deployable to a standard + container (Tomcat included). An executable JAR will not work because + of a hard coded file patter in Tomcat itself. + +* Jetty does not currently work as an embedded container with + JSPs. There should be a way to make it work, so hopefully someone + can figure it out (pull requests always welcome). -> org.apache.jasper.JasperException: The absolute uri: http://www.springframework.org/tags -> cannot be resolved in either web.xml or the jar files deployed with this application - -When packaged and run as an executable WAR, you will see an error message produced during -application startup that is similar to the following: - -> java.io.FileNotFoundException: JAR entry -> WEB-INF/lib-provided/tomcat-embed-jasper-7.0.47.jar!/javax/servlet/jsp/resources/web-jsptaglibrary_1_2.dtd -> not found in my-app.war - -To avoid these limitations, rather than using the embedded servlet container support, -package your application as a traditional (non-executable) WAR file and deploy it to a -servlet container. Alternatively, you may want to consider using an alternative view -technology. +There is a +[JSP sample](https://github.com/spring-projects/spring-boot/tree/master/spring-boot-samples/spring-boot-sample-web-jsp) +so you can see how to set things up. ## Using YAML instead of Properties [YAML](http://yaml.org) is a superset of JSON, and as such is a very convenient format diff --git a/spring-boot/src/main/java/org/springframework/boot/context/embedded/AbstractEmbeddedServletContainerFactory.java b/spring-boot/src/main/java/org/springframework/boot/context/embedded/AbstractEmbeddedServletContainerFactory.java index 72916108dd..21619ea157 100644 --- a/spring-boot/src/main/java/org/springframework/boot/context/embedded/AbstractEmbeddedServletContainerFactory.java +++ b/spring-boot/src/main/java/org/springframework/boot/context/embedded/AbstractEmbeddedServletContainerFactory.java @@ -304,7 +304,7 @@ public abstract class AbstractEmbeddedServletContainerFactory implements // Or maybe there is a document root in a well-known location file = file != null ? file : getCommonDocumentRoot(); if (file == null && this.logger.isWarnEnabled()) { - this.logger.debug("None of the document roots " + this.logger.info("None of the document roots " + Arrays.asList(COMMON_DOC_ROOTS) + " point to a directory and will be ignored."); } @@ -340,7 +340,11 @@ public abstract class AbstractEmbeddedServletContainerFactory implements } private File getWarFileDocumentRoot() { - return getArchiveFileDocumentRoot(".war"); + File file = getArchiveFileDocumentRoot(".war"); + if (file != null) { + return file; + } + return getArchiveFileDocumentRoot(".jar"); } private File getCommonDocumentRoot() { diff --git a/spring-boot/src/main/java/org/springframework/boot/context/embedded/jetty/JettyEmbeddedServletContainerFactory.java b/spring-boot/src/main/java/org/springframework/boot/context/embedded/jetty/JettyEmbeddedServletContainerFactory.java index 829299b917..d470e096a6 100644 --- a/spring-boot/src/main/java/org/springframework/boot/context/embedded/jetty/JettyEmbeddedServletContainerFactory.java +++ b/spring-boot/src/main/java/org/springframework/boot/context/embedded/jetty/JettyEmbeddedServletContainerFactory.java @@ -131,7 +131,12 @@ public class JettyEmbeddedServletContainerFactory extends File root = getValidDocumentRoot(); if (root != null) { try { - handler.setBaseResource(Resource.newResource(root)); + if (!root.isDirectory()) { + handler.setBaseResource(Resource.newClassPathResource("")); + } + else { + handler.setBaseResource(Resource.newResource(root)); + } } catch (Exception ex) { throw new IllegalStateException(ex);