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

@@ -157,10 +157,12 @@ public class ScriptTemplateConfigurer implements ScriptTemplateConfig {
/**
* Set the render function name (mandatory).
* This function will be called with the following parameters:
*
* <p>This function will be called with the following parameters:
* <ol>
* <li>{@code template}: the view template content (String)</li>
* <li>{@code model}: the view model (Map)</li>
* <li>{@code String template}: the template content</li>
* <li>{@code Map model}: the view model</li>
* <li>{@code String url}: the template url (since 4.2.2)</li>
* </ol>
*/
public void setRenderFunction(String renderFunction) {

View File

@@ -323,14 +323,15 @@ public class ScriptTemplateView extends AbstractUrlBasedView {
try {
ScriptEngine engine = getEngine();
Invocable invocable = (Invocable) engine;
String template = getTemplate(getUrl());
String url = getUrl();
String template = getTemplate(url);
Object html;
if (this.renderObject != null) {
Object thiz = engine.eval(this.renderObject);
html = invocable.invokeMethod(thiz, this.renderFunction, template, model);
html = invocable.invokeMethod(thiz, this.renderFunction, template, model, url);
}
else {
html = invocable.invokeFunction(this.renderFunction, template, model);
html = invocable.invokeFunction(this.renderFunction, template, model, url);
}
response.getWriter().write(String.valueOf(html));
}