From c8ca2495deed4c8750f060e234d51a02e49686e8 Mon Sep 17 00:00:00 2001 From: Dave Syer Date: Fri, 16 May 2014 13:03:50 +0100 Subject: [PATCH] Use URLs for groovy template resources --- .../template/GroovyTemplateAutoConfiguration.java | 15 +++++++++------ .../template/web/GroovyTemplateViewResolver.java | 13 +------------ 2 files changed, 10 insertions(+), 18 deletions(-) diff --git a/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/groovy/template/GroovyTemplateAutoConfiguration.java b/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/groovy/template/GroovyTemplateAutoConfiguration.java index a2780dbcfab..818f7f47ec2 100644 --- a/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/groovy/template/GroovyTemplateAutoConfiguration.java +++ b/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/groovy/template/GroovyTemplateAutoConfiguration.java @@ -36,6 +36,7 @@ import org.springframework.boot.autoconfigure.groovy.template.web.GroovyTemplate import org.springframework.boot.autoconfigure.groovy.template.web.LocaleAwareTemplate; import org.springframework.boot.autoconfigure.web.WebMvcAutoConfiguration; import org.springframework.boot.context.properties.EnableConfigurationProperties; +import org.springframework.context.ApplicationContext; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.context.i18n.LocaleContextHolder; @@ -70,7 +71,7 @@ public class GroovyTemplateAutoConfiguration { public static class GroovyWebConfiguration implements BeanClassLoaderAware { @Autowired - private final ResourceLoader resourceLoader = new DefaultResourceLoader(); + private ApplicationContext resourceLoader; @Autowired private GroovyTemplateProperties properties; @@ -95,11 +96,14 @@ public class GroovyTemplateAutoConfiguration { } private ClassLoader createParentLoaderForTemplates() throws Exception { - Resource resource = this.resourceLoader.getResource(this.properties + Resource[] resources = this.resourceLoader.getResources(this.properties .getPrefix()); - if (resource.exists()) { - return new URLClassLoader(new URL[] { resource.getURL() }, - this.classLoader); + if (resources.length > 0) { + URL[] urls = new URL[resources.length]; + for (int i = 0; i < resources.length; i++) { + urls[i] = resources[i].getURL(); + } + return new URLClassLoader(urls, this.classLoader); } else { return this.classLoader; @@ -114,7 +118,6 @@ public class GroovyTemplateAutoConfiguration { resolver.setSuffix(this.properties.getSuffix()); resolver.setCache(this.properties.isCache()); resolver.setContentType(this.properties.getContentType()); - resolver.setCharSet(this.properties.getCharSet()); resolver.setViewNames(this.properties.getViewNames()); resolver.setTemplateEngine(engine); diff --git a/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/groovy/template/web/GroovyTemplateViewResolver.java b/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/groovy/template/web/GroovyTemplateViewResolver.java index 3f2d07ff23e..d507a8f3995 100644 --- a/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/groovy/template/web/GroovyTemplateViewResolver.java +++ b/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/groovy/template/web/GroovyTemplateViewResolver.java @@ -20,7 +20,6 @@ import groovy.text.SimpleTemplateEngine; import groovy.text.Template; import groovy.text.TemplateEngine; -import java.io.InputStreamReader; import java.util.Locale; import org.springframework.beans.propertyeditors.LocaleEditor; @@ -36,8 +35,6 @@ public class GroovyTemplateViewResolver extends UrlBasedViewResolver { private TemplateEngine engine = new SimpleTemplateEngine(); - private String charSet = "UTF-8"; - public GroovyTemplateViewResolver() { setViewClass(GroovyTemplateView.class); } @@ -49,21 +46,13 @@ public class GroovyTemplateViewResolver extends UrlBasedViewResolver { this.engine = engine; } - /** - * @param charSet the charSet to set - */ - public void setCharSet(String charSet) { - this.charSet = charSet; - } - @Override protected View loadView(String viewName, Locale locale) throws Exception { Resource resource = resolveResource(viewName, locale); if (resource == null) { return null; } - Template template = this.engine.createTemplate(new InputStreamReader(resource - .getInputStream(), this.charSet)); + Template template = this.engine.createTemplate(resource.getURL()); GroovyTemplateView view = new GroovyTemplateView(template); view.setApplicationContext(getApplicationContext()); view.setServletContext(getServletContext());