Nashorn/Rhino to convert a string from Java to Javascript -
i'm using play framework , have .java controller file in obtain array of strings. want pass java array html file use javascript in order plot data using flot charts. data "transfer" done in render. this:
string[] array = new string[list.size()]; int = 0; (sample sample : list) { array[i++] = sample.getcontent(); } render(array);
but when i'm unable call variable in .html file inside views folder. if use ${array}, firebug tells me not recognize valid js string array. i've read rhino or nashorn trick, not know if best , simplest option. ideas? thanks!
i'm not familiar play framework i'm doing similar stuff using sparkjava in both java , javascript (using nashorn).
i suggest use boon library generate json: https://github.com/boonproject/boon.
here's small nashorn snippet speed, adaptable java:
// 1st create factory serialize json out var jso = new org.boon.json.jsonserializerfactory().create(); // 2nd directly use boon on array variable. boon supports out of box many pure java objects jso.serialize(o);
in specific case, you'll need configure play output particular render application/json
, possibly use render(jso.serialize(o));
in place of small snippet gave.
Comments
Post a Comment