From b1d8cc55fcc7b354d056707cd820c6f1c19e718d Mon Sep 17 00:00:00 2001 From: Andy Wilkinson Date: Thu, 21 Jun 2018 17:19:53 +0100 Subject: [PATCH] Polish "Fix JSP availability check when not running as a packaged war" Closes gh-12859 --- .../web/JspTemplateAvailabilityProvider.java | 10 +++++++--- .../src/main/webapp/WEB-INF/jsp/error.jsp | 6 ++++++ .../jsp/SampleWebJspApplicationTests.java | 20 ++++++++++++++++++- 3 files changed, 32 insertions(+), 4 deletions(-) create mode 100644 spring-boot-samples/spring-boot-sample-web-jsp/src/main/webapp/WEB-INF/jsp/error.jsp diff --git a/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/web/JspTemplateAvailabilityProvider.java b/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/web/JspTemplateAvailabilityProvider.java index e2088b95534..01c95deb58b 100755 --- a/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/web/JspTemplateAvailabilityProvider.java +++ b/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/web/JspTemplateAvailabilityProvider.java @@ -1,5 +1,5 @@ /* - * Copyright 2012-2016 the original author or authors. + * Copyright 2012-2018 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. @@ -16,6 +16,8 @@ package org.springframework.boot.autoconfigure.web; +import java.io.File; + import org.springframework.boot.autoconfigure.template.TemplateAvailabilityProvider; import org.springframework.boot.bind.RelaxedPropertyResolver; import org.springframework.core.env.Environment; @@ -38,8 +40,10 @@ public class JspTemplateAvailabilityProvider implements TemplateAvailabilityProv ClassLoader classLoader, ResourceLoader resourceLoader) { if (ClassUtils.isPresent("org.apache.jasper.compiler.JspConfig", classLoader)) { String resourceName = getResourceName(view, environment); - return resourceLoader.getResource(resourceName).exists() || - resourceLoader.getResource("file:./src/main/webapp" + resourceName).exists(); + if (resourceLoader.getResource(resourceName).exists()) { + return true; + } + return new File("src/main/webapp", resourceName).exists(); } return false; } diff --git a/spring-boot-samples/spring-boot-sample-web-jsp/src/main/webapp/WEB-INF/jsp/error.jsp b/spring-boot-samples/spring-boot-sample-web-jsp/src/main/webapp/WEB-INF/jsp/error.jsp new file mode 100644 index 00000000000..68433f08fac --- /dev/null +++ b/spring-boot-samples/spring-boot-sample-web-jsp/src/main/webapp/WEB-INF/jsp/error.jsp @@ -0,0 +1,6 @@ + + + +Something went wrong: ${status} ${error} + + \ No newline at end of file diff --git a/spring-boot-samples/spring-boot-sample-web-jsp/src/test/java/sample/jsp/SampleWebJspApplicationTests.java b/spring-boot-samples/spring-boot-sample-web-jsp/src/test/java/sample/jsp/SampleWebJspApplicationTests.java index 824e18fcde1..552b73ebab1 100644 --- a/spring-boot-samples/spring-boot-sample-web-jsp/src/test/java/sample/jsp/SampleWebJspApplicationTests.java +++ b/spring-boot-samples/spring-boot-sample-web-jsp/src/test/java/sample/jsp/SampleWebJspApplicationTests.java @@ -1,5 +1,5 @@ /* - * Copyright 2012-2016 the original author or authors. + * Copyright 2012-2018 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. @@ -16,6 +16,9 @@ package sample.jsp; +import java.net.URI; +import java.util.Arrays; + import org.junit.Test; import org.junit.runner.RunWith; @@ -23,7 +26,11 @@ import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.context.SpringBootTest; import org.springframework.boot.test.context.SpringBootTest.WebEnvironment; import org.springframework.boot.test.web.client.TestRestTemplate; +import org.springframework.http.HttpHeaders; +import org.springframework.http.HttpMethod; import org.springframework.http.HttpStatus; +import org.springframework.http.MediaType; +import org.springframework.http.RequestEntity; import org.springframework.http.ResponseEntity; import org.springframework.test.annotation.DirtiesContext; import org.springframework.test.context.junit4.SpringRunner; @@ -50,4 +57,15 @@ public class SampleWebJspApplicationTests { assertThat(entity.getBody()).contains("/resources/text.txt"); } + @Test + public void customErrorPage() throws Exception { + HttpHeaders headers = new HttpHeaders(); + headers.setAccept(Arrays.asList(MediaType.TEXT_HTML)); + RequestEntity request = new RequestEntity<>(headers, HttpMethod.GET, + URI.create("/foo")); + ResponseEntity entity = this.restTemplate.exchange(request, String.class); + assertThat(entity.getStatusCode()).isEqualTo(HttpStatus.INTERNAL_SERVER_ERROR); + assertThat(entity.getBody()).contains("Something went wrong"); + } + }