Skip to content

Commit b1d8cc5

Browse files
committed
Polish "Fix JSP availability check when not running as a packaged war"
Closes gh-12859
1 parent 82465cf commit b1d8cc5

File tree

3 files changed

+32
-4
lines changed

3 files changed

+32
-4
lines changed

spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/web/JspTemplateAvailabilityProvider.java

+7-3
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2016 the original author or authors.
2+
* Copyright 2012-2018 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -16,6 +16,8 @@
1616

1717
package org.springframework.boot.autoconfigure.web;
1818

19+
import java.io.File;
20+
1921
import org.springframework.boot.autoconfigure.template.TemplateAvailabilityProvider;
2022
import org.springframework.boot.bind.RelaxedPropertyResolver;
2123
import org.springframework.core.env.Environment;
@@ -38,8 +40,10 @@ public boolean isTemplateAvailable(String view, Environment environment,
3840
ClassLoader classLoader, ResourceLoader resourceLoader) {
3941
if (ClassUtils.isPresent("org.apache.jasper.compiler.JspConfig", classLoader)) {
4042
String resourceName = getResourceName(view, environment);
41-
return resourceLoader.getResource(resourceName).exists() ||
42-
resourceLoader.getResource("file:./src/main/webapp" + resourceName).exists();
43+
if (resourceLoader.getResource(resourceName).exists()) {
44+
return true;
45+
}
46+
return new File("src/main/webapp", resourceName).exists();
4347
}
4448
return false;
4549
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
<!DOCTYPE html>
2+
<html lang="en">
3+
<body>
4+
Something went wrong: ${status} ${error}
5+
</body>
6+
</html>

spring-boot-samples/spring-boot-sample-web-jsp/src/test/java/sample/jsp/SampleWebJspApplicationTests.java

+19-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2016 the original author or authors.
2+
* Copyright 2012-2018 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -16,14 +16,21 @@
1616

1717
package sample.jsp;
1818

19+
import java.net.URI;
20+
import java.util.Arrays;
21+
1922
import org.junit.Test;
2023
import org.junit.runner.RunWith;
2124

2225
import org.springframework.beans.factory.annotation.Autowired;
2326
import org.springframework.boot.test.context.SpringBootTest;
2427
import org.springframework.boot.test.context.SpringBootTest.WebEnvironment;
2528
import org.springframework.boot.test.web.client.TestRestTemplate;
29+
import org.springframework.http.HttpHeaders;
30+
import org.springframework.http.HttpMethod;
2631
import org.springframework.http.HttpStatus;
32+
import org.springframework.http.MediaType;
33+
import org.springframework.http.RequestEntity;
2734
import org.springframework.http.ResponseEntity;
2835
import org.springframework.test.annotation.DirtiesContext;
2936
import org.springframework.test.context.junit4.SpringRunner;
@@ -50,4 +57,15 @@ public void testJspWithEl() throws Exception {
5057
assertThat(entity.getBody()).contains("/resources/text.txt");
5158
}
5259

60+
@Test
61+
public void customErrorPage() throws Exception {
62+
HttpHeaders headers = new HttpHeaders();
63+
headers.setAccept(Arrays.asList(MediaType.TEXT_HTML));
64+
RequestEntity<Void> request = new RequestEntity<>(headers, HttpMethod.GET,
65+
URI.create("/foo"));
66+
ResponseEntity<String> entity = this.restTemplate.exchange(request, String.class);
67+
assertThat(entity.getStatusCode()).isEqualTo(HttpStatus.INTERNAL_SERVER_ERROR);
68+
assertThat(entity.getBody()).contains("Something went wrong");
69+
}
70+
5371
}

0 commit comments

Comments
 (0)