Expose view url to render function in ScriptTemplateView

After this change, with Nashorn it is possible to use either
render(template, model) or render(template, model, url).
With JRuby or Jython, specifying the 3 parameters is mandatory.

Issue: SPR-13453
This commit is contained in:
Sebastien Deleuze
2015-09-09 18:05:04 +02:00
parent ff02ad47e0
commit f3b7e9ff2d
7 changed files with 46 additions and 17 deletions

View File

@@ -56,22 +56,31 @@ public class NashornScriptTemplateTests {
Map<String, Object> model = new HashMap<>();
model.put("title", "Layout example");
model.put("body", "This is the body");
MockHttpServletResponse response = renderViewWithModel("org/springframework/web/servlet/view/script/nashorn/template.html", model);
MockHttpServletResponse response = renderViewWithModel("org/springframework/web/servlet/view/script/nashorn/template.html",
model, ScriptTemplatingConfiguration.class);
assertEquals("<html><head><title>Layout example</title></head><body><p>This is the body</p></body></html>",
response.getContentAsString());
}
private MockHttpServletResponse renderViewWithModel(String viewUrl, Map<String, Object> model) throws Exception {
ScriptTemplateView view = createViewWithUrl(viewUrl);
@Test // SPR-13453
public void renderTemplateWithUrl() throws Exception {
MockHttpServletResponse response = renderViewWithModel("org/springframework/web/servlet/view/script/nashorn/template.html",
null, ScriptTemplatingWithUrlConfiguration.class);
assertEquals("<html><head><title>Check url parameter</title></head><body><p>org/springframework/web/servlet/view/script/nashorn/template.html</p></body></html>",
response.getContentAsString());
}
private MockHttpServletResponse renderViewWithModel(String viewUrl, Map<String, Object> model, Class<?> configuration) throws Exception {
ScriptTemplateView view = createViewWithUrl(viewUrl, configuration);
MockHttpServletResponse response = new MockHttpServletResponse();
MockHttpServletRequest request = new MockHttpServletRequest();
view.renderMergedOutputModel(model, request, response);
return response;
}
private ScriptTemplateView createViewWithUrl(String viewUrl) throws Exception {
private ScriptTemplateView createViewWithUrl(String viewUrl, Class<?> configuration) throws Exception {
AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext();
ctx.register(ScriptTemplatingConfiguration.class);
ctx.register(configuration);
ctx.refresh();
ScriptTemplateView view = new ScriptTemplateView();
@@ -94,4 +103,17 @@ public class NashornScriptTemplateTests {
}
}
@Configuration
static class ScriptTemplatingWithUrlConfiguration {
@Bean
public ScriptTemplateConfigurer nashornConfigurer() {
ScriptTemplateConfigurer configurer = new ScriptTemplateConfigurer();
configurer.setEngineName("nashorn");
configurer.setScripts("org/springframework/web/servlet/view/script/nashorn/render.js");
configurer.setRenderFunction("renderWithUrl");
return configurer;
}
}
}