Consistent ScriptTemplateView locale resolution via request
Issue: SPR-15064
This commit is contained in:
@@ -40,6 +40,7 @@ public class RenderingContext {
|
||||
|
||||
public RenderingContext(ApplicationContext applicationContext, Locale locale,
|
||||
Function<String, String> templateLoader, String url) {
|
||||
|
||||
this.applicationContext = applicationContext;
|
||||
this.locale = locale;
|
||||
this.templateLoader = templateLoader;
|
||||
@@ -48,19 +49,19 @@ public class RenderingContext {
|
||||
|
||||
|
||||
public ApplicationContext getApplicationContext() {
|
||||
return applicationContext;
|
||||
return this.applicationContext;
|
||||
}
|
||||
|
||||
public Locale getLocale() {
|
||||
return locale;
|
||||
return this.locale;
|
||||
}
|
||||
|
||||
public Function<String, String> getTemplateLoader() {
|
||||
return templateLoader;
|
||||
return this.templateLoader;
|
||||
}
|
||||
|
||||
public String getUrl() {
|
||||
return url;
|
||||
return this.url;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -49,6 +49,7 @@ import org.springframework.util.Assert;
|
||||
import org.springframework.util.FileCopyUtils;
|
||||
import org.springframework.util.ObjectUtils;
|
||||
import org.springframework.util.StringUtils;
|
||||
import org.springframework.web.servlet.support.RequestContextUtils;
|
||||
import org.springframework.web.servlet.view.AbstractUrlBasedView;
|
||||
|
||||
/**
|
||||
@@ -59,7 +60,7 @@ import org.springframework.web.servlet.view.AbstractUrlBasedView;
|
||||
* {@link ScriptTemplateConfig} bean in the web application context and using
|
||||
* it to obtain the configured properties.
|
||||
*
|
||||
* <p>Nashorn Javascript engine requires Java 8+, and may require setting the
|
||||
* <p>The Nashorn JavaScript engine requires Java 8+ and may require setting the
|
||||
* {@code sharedEngine} property to {@code false} in order to run properly. See
|
||||
* {@link ScriptTemplateConfigurer#setSharedEngine(Boolean)} for more details.
|
||||
*
|
||||
@@ -86,8 +87,6 @@ public class ScriptTemplateView extends AbstractUrlBasedView {
|
||||
|
||||
private String engineName;
|
||||
|
||||
private Locale locale;
|
||||
|
||||
private Boolean sharedEngine;
|
||||
|
||||
private String[] scripts;
|
||||
@@ -130,14 +129,6 @@ public class ScriptTemplateView extends AbstractUrlBasedView {
|
||||
this.engine = engine;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the {@link Locale} to pass to the render function.
|
||||
* @since 5.0
|
||||
*/
|
||||
public void setLocale(Locale locale) {
|
||||
this.locale = locale;
|
||||
}
|
||||
|
||||
/**
|
||||
* See {@link ScriptTemplateConfigurer#setEngineName(String)} documentation.
|
||||
*/
|
||||
@@ -362,6 +353,7 @@ public class ScriptTemplateView extends AbstractUrlBasedView {
|
||||
String url = getUrl();
|
||||
Assert.state(url != null, "'url' not set");
|
||||
String template = getTemplate(url);
|
||||
|
||||
Function<String, String> templateLoader = path -> {
|
||||
try {
|
||||
return getTemplate(path);
|
||||
@@ -370,7 +362,9 @@ public class ScriptTemplateView extends AbstractUrlBasedView {
|
||||
throw new IllegalStateException(ex);
|
||||
}
|
||||
};
|
||||
RenderingContext context = new RenderingContext(obtainApplicationContext(), this.locale, templateLoader, url);
|
||||
|
||||
Locale locale = RequestContextUtils.getLocale(request);
|
||||
RenderingContext context = new RenderingContext(obtainApplicationContext(), locale, templateLoader, url);
|
||||
|
||||
Object html;
|
||||
if (this.renderFunction == null) {
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2002-2015 the original author or authors.
|
||||
* Copyright 2002-2017 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,9 +16,6 @@
|
||||
|
||||
package org.springframework.web.servlet.view.script;
|
||||
|
||||
import java.util.Locale;
|
||||
|
||||
import org.springframework.web.servlet.View;
|
||||
import org.springframework.web.servlet.view.UrlBasedViewResolver;
|
||||
|
||||
/**
|
||||
@@ -59,14 +56,6 @@ public class ScriptTemplateViewResolver extends UrlBasedViewResolver {
|
||||
setSuffix(suffix);
|
||||
}
|
||||
|
||||
@Override
|
||||
public View resolveViewName(String viewName, Locale locale) throws Exception {
|
||||
ScriptTemplateView view = (ScriptTemplateView)super.resolveViewName(viewName, locale);
|
||||
if (view != null) {
|
||||
view.setLocale(locale);
|
||||
}
|
||||
return view;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected Class<?> requiredViewClass() {
|
||||
|
||||
Reference in New Issue
Block a user