function render(template, model) {
    var data = toJsonObject(model);
    return new EJS({text: template}).render({data: data});
}

/* Create a real JSON object from the model Map . You can't pass the model directly
 * into EJS - it blows up. */
function toJsonObject(model) {
    var o = {}; 
    for (var k in model) { 
        // Convert Iterable like List to real JSON array
        if (model[k] instanceof Java.type("java.lang.Iterable")) {
            o[k] = Java.from(model[k]);
        }
        else {
            o[k] = model[k]; 
        } 
    }  
    return o;
}
var render = (function () {

    // http://www.slideshare.net/SpringCentral/serverside-javascript-with-nashorn-and-spring
    var serializer = (new Packages.com.fasterxml.jackson.databind.ObjectMapper()).writer();

    return function(template, model) {
        var json =  serializer.writeValueAsString(model);

        return new EJS({text: template}).render({data: JSON.parse(json), json: json});
    };
})();
Switch to Jackson for serialising client state
Switch to Jackson for serialising client state

See the following - Nashorn's JSON object doesn't handle POJOs well.

http://www.slideshare.net/SpringCentral/serverside-javascript-with-nashorn-and-spring
