Fix Jasper on Tomcat 8

Refactor Jasper initialization code to call JasperInitializer during
server start.

Fixes gh-962
See gh-919
This commit is contained in:
Phillip Webb 2014-05-28 14:52:21 +01:00
parent 790e615741
commit 329175b956
10 changed files with 246 additions and 62 deletions

View File

@ -39,6 +39,7 @@
<module>spring-boot-sample-simple</module>
<module>spring-boot-sample-tomcat</module>
<module>spring-boot-sample-tomcat-multi-connectors</module>
<module>spring-boot-sample-tomcat8-jsp</module>
<module>spring-boot-sample-traditional</module>
<module>spring-boot-sample-web-method-security</module>
<module>spring-boot-sample-web-secure</module>

View File

@ -0,0 +1,65 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<!-- Your own application should inherit from spring-boot-starter-parent -->
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-samples</artifactId>
<version>1.0.3.BUILD-SNAPSHOT</version>
</parent>
<artifactId>spring-boot-sample-tomcat8-jsp</artifactId>
<packaging>war</packaging>
<name>Spring Boot Tomcat 8 JSP Sample</name>
<description>Spring Boot Tomcat 8 JSP Sample</description>
<url>http://projects.spring.io/spring-boot/</url>
<organization>
<name>Pivotal Software, Inc.</name>
<url>http://www.spring.io</url>
</organization>
<properties>
<main.basedir>${basedir}/../..</main.basedir>
<m2eclipse.wtp.contextRoot>/</m2eclipse.wtp.contextRoot>
<tomcat.version>8.0.8</tomcat.version>
<java.version>1.7</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.apache.tomcat.embed</groupId>
<artifactId>tomcat-embed-jasper</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jstl</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<configuration>
<useSystemClassLoader>false</useSystemClassLoader>
</configuration>
</plugin>
</plugins>
</build>
</project>

View File

@ -0,0 +1,40 @@
/*
* Copyright 2012-2013 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package sample.jsp;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.boot.context.web.SpringBootServletInitializer;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
@Configuration
@EnableAutoConfiguration
@ComponentScan
public class SampleTomcat8JspApplication extends SpringBootServletInitializer {
@Override
protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
return application.sources(SampleTomcat8JspApplication.class);
}
public static void main(String[] args) throws Exception {
SpringApplication.run(SampleTomcat8JspApplication.class, args);
}
}

View File

@ -0,0 +1,39 @@
/*
* Copyright 2012-2014 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package sample.jsp;
import java.util.Date;
import java.util.Map;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
@Controller
public class WelcomeController {
@Value("${application.message:Hello World}")
private String message = "Hello World";
@RequestMapping("/")
public String welcome(Map<String, Object> model) {
model.put("time", new Date());
model.put("message", this.message);
return "welcome";
}
}

View File

@ -0,0 +1,3 @@
spring.view.prefix: /WEB-INF/jsp/
spring.view.suffix: .jsp
application.message: Hello Phil

View File

@ -0,0 +1,18 @@
<!DOCTYPE html>
<%@ taglib prefix="spring" uri="http://www.springframework.org/tags"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<html lang="en">
<body>
<c:url value="/resources/text.txt" var="url"/>
<spring:url value="/resources/text.txt" htmlEscape="true" var="springUrl" />
Spring URL: ${springUrl} at ${time}
<br>
JSTL URL: ${url}
<br>
Message: ${message}
</body>
</html>

View File

@ -0,0 +1,58 @@
/*
* Copyright 2012-2014 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package sample.jsp;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.test.IntegrationTest;
import org.springframework.boot.test.SpringApplicationConfiguration;
import org.springframework.boot.test.TestRestTemplate;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.test.annotation.DirtiesContext;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.test.context.web.WebAppConfiguration;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
/**
* Basic integration tests for JSP application.
*
* @author Phillip Webb
*/
@RunWith(SpringJUnit4ClassRunner.class)
@SpringApplicationConfiguration(classes = SampleTomcat8JspApplication.class)
@WebAppConfiguration
@IntegrationTest("server.port:0")
@DirtiesContext
public class SampleWebJspApplicationTests {
@Value("${local.server.port}")
private int port;
@Test
public void testJspWithEl() throws Exception {
ResponseEntity<String> entity = new TestRestTemplate().getForEntity(
"http://localhost:" + this.port, String.class);
assertEquals(HttpStatus.OK, entity.getStatusCode());
assertTrue("Wrong body:\n" + entity.getBody(),
entity.getBody().contains("/resources/text.txt"));
}
}

View File

@ -25,15 +25,18 @@ import javax.servlet.ServletContext;
import org.apache.tomcat.JarScanner;
import org.apache.tomcat.JarScannerCallback;
import org.apache.tomcat.util.scan.StandardJarScanner;
import org.springframework.util.Assert;
/**
* {@link JarScanner} decorator allowing alternative default jar pattern matching.
* {@link JarScanner} decorator allowing alternative default jar pattern matching. This
* class extends {@link StandardJarScanner} rather than implementing the
* {@link JarScanner} due to API changes introduced in Tomcat 8.
*
* @author Phillip Webb
* @see #apply(TomcatEmbeddedContext, String)
*/
class SkipPatternJarScanner implements JarScanner {
class SkipPatternJarScanner extends StandardJarScanner {
private final JarScanner jarScanner;
@ -58,7 +61,9 @@ class SkipPatternJarScanner implements JarScanner {
* @param pattern the jar skip pattern or {@code null} for defaults
*/
public static void apply(TomcatEmbeddedContext context, String pattern) {
context.setJarScanner(new SkipPatternJarScanner(context.getJarScanner(), pattern));
SkipPatternJarScanner scanner = new SkipPatternJarScanner(
context.getJarScanner(), pattern);
context.setJarScanner(scanner);
}
private static class SkipPattern {

View File

@ -1,58 +0,0 @@
package org.springframework.boot.context.embedded.tomcat;
import javax.servlet.ServletContainerInitializer;
import javax.servlet.ServletException;
import org.apache.catalina.Lifecycle;
import org.apache.catalina.LifecycleEvent;
import org.apache.catalina.LifecycleListener;
import org.apache.catalina.core.StandardContext;
import org.springframework.util.Assert;
import org.springframework.util.ClassUtils;
/**
* Tomcat {@link LifecycleListener} to initialize Jasper by calling the
* `JasperInitializer` used in Tomcat 8.
*
* @author Phillip Webb
*/
class JasperInitializerLifecycleListener implements LifecycleListener {
private static final String JASPER_INITIALIZER_CLASS = "org.apache.jasper.servlet.JasperInitializer";
private ServletContainerInitializer initializer;
public JasperInitializerLifecycleListener() {
this.initializer = getJasperInitializer();
}
@Override
public void lifecycleEvent(LifecycleEvent event) {
if (this.initializer != null
&& Lifecycle.CONFIGURE_START_EVENT.equals(event.getType())) {
onStartup(event);
}
}
private void onStartup(LifecycleEvent event) {
Assert.isInstanceOf(StandardContext.class, event.getSource());
StandardContext standardContext = (StandardContext) event.getSource();
try {
this.initializer.onStartup(null, standardContext.getServletContext());
}
catch (ServletException ex) {
throw new IllegalStateException(ex);
}
}
private ServletContainerInitializer getJasperInitializer() {
try {
Class<?> jasperClass = ClassUtils.forName(JASPER_INITIALIZER_CLASS, null);
return (ServletContainerInitializer) jasperClass.newInstance();
}
catch (Exception ex) {
return null;
}
}
}

View File

@ -26,6 +26,7 @@ import java.util.Arrays;
import java.util.Collection;
import java.util.List;
import javax.servlet.ServletContainerInitializer;
import javax.servlet.ServletContext;
import org.apache.catalina.Context;
@ -167,6 +168,7 @@ public class TomcatEmbeddedServletContainerFactory extends
&& ClassUtils.isPresent(getJspServletClassName(), getClass()
.getClassLoader())) {
addJspServlet(context);
addJasperInitializer(context);
context.addLifecycleListener(new StoreMergedWebXmlListener());
}
ServletContextInitializer[] initializersToUse = mergeInitializers(initializers);
@ -199,6 +201,18 @@ public class TomcatEmbeddedServletContainerFactory extends
context.addServletMapping("*.jspx", "jsp");
}
private void addJasperInitializer(TomcatEmbeddedContext context) {
try {
ServletContainerInitializer initializer = (ServletContainerInitializer) ClassUtils
.forName("org.apache.jasper.servlet.JasperInitializer", null)
.newInstance();
context.addServletContainerInitializer(initializer, null);
}
catch (Exception ex) {
// Probably not Tomcat 8
}
}
// Needs to be protected so it can be used by subclasses
protected void customizeConnector(Connector connector) {
int port = (getPort() >= 0 ? getPort() : 0);
@ -230,7 +244,6 @@ public class TomcatEmbeddedServletContainerFactory extends
ServletContextInitializer[] initializers) {
context.addLifecycleListener(new ServletContextInitializerLifecycleListener(
initializers));
context.addLifecycleListener(new JasperInitializerLifecycleListener());
for (LifecycleListener lifecycleListener : this.contextLifecycleListeners) {
context.addLifecycleListener(lifecycleListener);
}