Add resolveTemplate method to Groovy markup configurer
This commit is contained in:
parent
0ecfa8e404
commit
5b02222a9f
|
@ -33,5 +33,6 @@ public interface GroovyMarkupConfig {
|
||||||
* May be unique to one servlet, or shared in the root context.
|
* May be unique to one servlet, or shared in the root context.
|
||||||
* @return the Groovy Template engine
|
* @return the Groovy Template engine
|
||||||
*/
|
*/
|
||||||
public MarkupTemplateEngine getTemplateEngine();
|
MarkupTemplateEngine getTemplateEngine();
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -155,7 +155,7 @@ public class GroovyMarkupConfigurer extends TemplateConfiguration
|
||||||
protected MarkupTemplateEngine createTemplateEngine() throws IOException {
|
protected MarkupTemplateEngine createTemplateEngine() throws IOException {
|
||||||
if (this.templateEngine == null) {
|
if (this.templateEngine == null) {
|
||||||
ClassLoader templateClassLoader = createTemplateClassLoader();
|
ClassLoader templateClassLoader = createTemplateClassLoader();
|
||||||
this.templateEngine = new MarkupTemplateEngine(templateClassLoader, this, createTemplateResolver());
|
this.templateEngine = new MarkupTemplateEngine(templateClassLoader, this, new LocaleTemplateResolver());
|
||||||
}
|
}
|
||||||
return this.templateEngine;
|
return this.templateEngine;
|
||||||
}
|
}
|
||||||
|
@ -182,30 +182,44 @@ public class GroovyMarkupConfigurer extends TemplateConfiguration
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Return a {@link TemplateResolver} to be used by the
|
* Resolve a template from the given template path.
|
||||||
* {@link MarkupTemplateEngine} to resolve template files.
|
|
||||||
*
|
*
|
||||||
* <p>The default implementation returns a resolver that uses the Locale
|
* <p>The default implementation uses the Locale associated with the current
|
||||||
* associated with the current request to resolve the relevant template file.
|
* request, as obtained through
|
||||||
* Effectively the locale configured at the engine level is ignored.
|
* {@link org.springframework.context.i18n.LocaleContextHolder LocaleContextHolder},
|
||||||
|
* to find the template file. Effectively the locale configured at the engine
|
||||||
|
* level is ignored.
|
||||||
*
|
*
|
||||||
* @see LocaleContextHolder
|
* @see LocaleContextHolder
|
||||||
* @see #setLocale(java.util.Locale)
|
* @see #setLocale(java.util.Locale)
|
||||||
|
*
|
||||||
|
* @param classLoader
|
||||||
|
* @param templatePath
|
||||||
|
* @return
|
||||||
|
* @throws IOException
|
||||||
*/
|
*/
|
||||||
protected TemplateResolver createTemplateResolver() {
|
protected URL resolveTemplate(ClassLoader classLoader, String templatePath) throws IOException {
|
||||||
return new MarkupTemplateResolver();
|
MarkupTemplateEngine.TemplateResource resource = MarkupTemplateEngine.TemplateResource.parse(templatePath);
|
||||||
|
Locale locale = LocaleContextHolder.getLocale();
|
||||||
|
URL url = classLoader.getResource(resource.withLocale(locale.toString().replace("-", "_")).toString());
|
||||||
|
if (url == null) {
|
||||||
|
url = classLoader.getResource(resource.withLocale(locale.getLanguage()).toString());
|
||||||
|
}
|
||||||
|
if (url == null) {
|
||||||
|
url = classLoader.getResource(resource.withLocale(null).toString());
|
||||||
|
}
|
||||||
|
if (url == null) {
|
||||||
|
throw new IOException("Unable to load template:" + templatePath);
|
||||||
|
}
|
||||||
|
return url;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* A custom {@link TemplateResolver template resolver} that resolves
|
* Custom {@link TemplateResolver template resolver} that simply delegates to
|
||||||
* templates using the locale found with LocaleContextHolder.
|
* {@link #resolveTemplate(ClassLoader, String)}..
|
||||||
*
|
|
||||||
* @author Cedric Champeau
|
|
||||||
* @author Brian Clozel
|
|
||||||
* @see LocaleContextHolder
|
|
||||||
*/
|
*/
|
||||||
private static class MarkupTemplateResolver implements TemplateResolver {
|
private class LocaleTemplateResolver implements TemplateResolver {
|
||||||
|
|
||||||
private ClassLoader classLoader;
|
private ClassLoader classLoader;
|
||||||
|
|
||||||
|
@ -215,20 +229,8 @@ public class GroovyMarkupConfigurer extends TemplateConfiguration
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public URL resolveTemplate(final String templatePath) throws IOException {
|
public URL resolveTemplate(String templatePath) throws IOException {
|
||||||
MarkupTemplateEngine.TemplateResource resource = MarkupTemplateEngine.TemplateResource.parse(templatePath);
|
return GroovyMarkupConfigurer.this.resolveTemplate(this.classLoader, templatePath);
|
||||||
Locale locale = LocaleContextHolder.getLocale();
|
|
||||||
URL url = this.classLoader.getResource(resource.withLocale(locale.toString().replace("-", "_")).toString());
|
|
||||||
if (url == null) {
|
|
||||||
url = this.classLoader.getResource(resource.withLocale(locale.getLanguage()).toString());
|
|
||||||
}
|
|
||||||
if (url == null) {
|
|
||||||
url = this.classLoader.getResource(resource.withLocale(null).toString());
|
|
||||||
}
|
|
||||||
if (url == null) {
|
|
||||||
throw new IOException("Unable to load template:" + templatePath);
|
|
||||||
}
|
|
||||||
return url;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue