Skip to content

Commit cc56ef5

Browse files
committed
Revised checkResource implementation
Issue: SPR-14729 (cherry picked from commit ca17edd)
1 parent 97c9b05 commit cc56ef5

File tree

2 files changed

+17
-27
lines changed

2 files changed

+17
-27
lines changed

spring-webmvc/src/main/java/org/springframework/web/servlet/view/script/ScriptTemplateView.java

Lines changed: 16 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2015 the original author or authors.
2+
* Copyright 2002-2016 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,7 +16,6 @@
1616

1717
package org.springframework.web.servlet.view.script;
1818

19-
import java.io.FileNotFoundException;
2019
import java.io.IOException;
2120
import java.io.InputStreamReader;
2221
import java.net.URL;
@@ -57,7 +56,7 @@
5756
* An {@link AbstractUrlBasedView} subclass designed to run any template library
5857
* based on a JSR-223 script engine.
5958
*
60-
* <p>If not set, each property is auto-detected by looking up up a single
59+
* <p>If not set, each property is auto-detected by looking up a single
6160
* {@link ScriptTemplateConfig} bean in the web application context and using
6261
* it to obtain the configured properties.
6362
*
@@ -189,19 +188,6 @@ public void setResourceLoaderPath(String resourceLoaderPath) {
189188
this.resourceLoaderPath = resourceLoaderPath;
190189
}
191190

192-
@Override
193-
public boolean checkResource(Locale locale) throws Exception {
194-
try {
195-
getTemplate(getUrl());
196-
return true;
197-
}
198-
catch (FileNotFoundException exc) {
199-
if (logger.isDebugEnabled()) {
200-
logger.debug("No ScriptTemplate view found for URL: " + getUrl());
201-
}
202-
return false;
203-
}
204-
}
205191

206192
@Override
207193
protected void initApplicationContext(ApplicationContext context) {
@@ -260,7 +246,6 @@ else if (this.engine != null) {
260246
Assert.isTrue(this.renderFunction != null, "The 'renderFunction' property must be defined.");
261247
}
262248

263-
264249
protected ScriptEngine getEngine() {
265250
if (Boolean.FALSE.equals(this.sharedEngine)) {
266251
Map<Object, ScriptEngine> engines = enginesHolder.get();
@@ -295,17 +280,17 @@ protected ScriptEngine createEngineFromName() {
295280

296281
protected void loadScripts(ScriptEngine engine) {
297282
if (!ObjectUtils.isEmpty(this.scripts)) {
298-
try {
299-
for (String script : this.scripts) {
300-
Resource resource = this.resourceLoader.getResource(script);
301-
if (!resource.exists()) {
302-
throw new IllegalStateException("Script resource [" + script + "] not found");
303-
}
283+
for (String script : this.scripts) {
284+
Resource resource = this.resourceLoader.getResource(script);
285+
if (!resource.exists()) {
286+
throw new IllegalStateException("Script resource [" + script + "] not found");
287+
}
288+
try {
304289
engine.eval(new InputStreamReader(resource.getInputStream()));
305290
}
306-
}
307-
catch (Exception ex) {
308-
throw new IllegalStateException("Failed to load script", ex);
291+
catch (Throwable ex) {
292+
throw new IllegalStateException("Failed to evaluate script [" + script + "]", ex);
293+
}
309294
}
310295
}
311296
}
@@ -345,6 +330,11 @@ protected ScriptTemplateConfig autodetectViewConfig() throws BeansException {
345330
}
346331

347332

333+
@Override
334+
public boolean checkResource(Locale locale) throws Exception {
335+
return this.resourceLoader.getResource(getUrl()).exists();
336+
}
337+
348338
@Override
349339
protected void prepareResponse(HttpServletRequest request, HttpServletResponse response) {
350340
super.prepareResponse(request, response);

spring-webmvc/src/test/java/org/springframework/web/servlet/view/script/ScriptTemplateViewTests.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,6 @@
4747
import org.springframework.web.context.support.StaticWebApplicationContext;
4848
import org.springframework.web.servlet.DispatcherServlet;
4949

50-
import static org.hamcrest.Matchers.*;
5150
import static org.junit.Assert.*;
5251
import static org.mockito.BDDMockito.*;
5352

@@ -70,6 +69,7 @@ public class ScriptTemplateViewTests {
7069
@Rule
7170
public ExpectedException expectedException = ExpectedException.none();
7271

72+
7373
@Before
7474
public void setup() {
7575
this.configurer = new ScriptTemplateConfigurer();

0 commit comments

Comments
 (0)