Support i18n and nested templates in ScriptTemplateView
This commit changes the 3rd parameter passed to the rendering function from String url to RenderingContext renderingContext. RenderingContext contains 4 properties: - ApplicationContext applicationContext - Locale locale - Function<String, String> templateLoader - String url Issue: SPR-15064
This commit is contained in:
@@ -17,6 +17,7 @@
|
||||
package org.springframework.web.servlet.view.script;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.Locale;
|
||||
import java.util.Map;
|
||||
import javax.servlet.ServletContext;
|
||||
|
||||
@@ -28,6 +29,7 @@ import static org.mockito.Mockito.mock;
|
||||
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.context.support.ResourceBundleMessageSource;
|
||||
import org.springframework.mock.web.test.MockHttpServletRequest;
|
||||
import org.springframework.mock.web.test.MockHttpServletResponse;
|
||||
import org.springframework.mock.web.test.MockServletContext;
|
||||
@@ -53,18 +55,28 @@ public class KotlinScriptTemplateTests {
|
||||
}
|
||||
|
||||
@Test
|
||||
public void renderTemplate() throws Exception {
|
||||
public void renderTemplateWithFrenchLocale() throws Exception {
|
||||
Map<String, Object> model = new HashMap<>();
|
||||
model.put("title", "Layout example");
|
||||
model.put("body", "This is the body");
|
||||
model.put("foo", "Foo");
|
||||
MockHttpServletResponse response = renderViewWithModel("org/springframework/web/servlet/view/script/kotlin/template.kts",
|
||||
model, ScriptTemplatingConfiguration.class);
|
||||
assertEquals("<html><head><title>Layout example</title></head><body><p>This is the body</p></body></html>",
|
||||
model, Locale.FRENCH, ScriptTemplatingConfiguration.class);
|
||||
assertEquals("<html><body>\n<p>Bonjour Foo</p>\n</body></html>",
|
||||
response.getContentAsString());
|
||||
}
|
||||
|
||||
private MockHttpServletResponse renderViewWithModel(String viewUrl, Map<String, Object> model, Class<?> configuration) throws Exception {
|
||||
@Test
|
||||
public void renderTemplateWithEnglishLocale() throws Exception {
|
||||
Map<String, Object> model = new HashMap<>();
|
||||
model.put("foo", "Foo");
|
||||
MockHttpServletResponse response = renderViewWithModel("org/springframework/web/servlet/view/script/kotlin/template.kts",
|
||||
model, Locale.ENGLISH, ScriptTemplatingConfiguration.class);
|
||||
assertEquals("<html><body>\n<p>Hello Foo</p>\n</body></html>",
|
||||
response.getContentAsString());
|
||||
}
|
||||
|
||||
private MockHttpServletResponse renderViewWithModel(String viewUrl, Map<String, Object> model, Locale locale, Class<?> configuration) throws Exception {
|
||||
ScriptTemplateView view = createViewWithUrl(viewUrl, configuration);
|
||||
view.setLocale(locale);
|
||||
MockHttpServletResponse response = new MockHttpServletResponse();
|
||||
MockHttpServletRequest request = new MockHttpServletRequest();
|
||||
view.renderMergedOutputModel(model, request, response);
|
||||
@@ -95,6 +107,13 @@ public class KotlinScriptTemplateTests {
|
||||
configurer.setRenderFunction("render");
|
||||
return configurer;
|
||||
}
|
||||
|
||||
@Bean
|
||||
public ResourceBundleMessageSource messageSource() {
|
||||
ResourceBundleMessageSource messageSource = new ResourceBundleMessageSource();
|
||||
messageSource.setBasename("org/springframework/web/servlet/view/script/messages");
|
||||
return messageSource;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user