@ -56,22 +56,31 @@ public class NashornScriptTemplateTests {
@@ -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 ( S criptTemplatingC onfiguration. class ) ;
ctx . register ( configuration ) ;
ctx . refresh ( ) ;
ScriptTemplateView view = new ScriptTemplateView ( ) ;
@ -94,4 +103,17 @@ public class NashornScriptTemplateTests {
@@ -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 ;
}
}
}