Java Reference
In-Depth Information
More than one Java type value can be passed in a SimpleBindings instance. In
the following example, three float values are passed in a single SimpleBindings
instance, and then they're passed to a JavaScript function.
float width = 16;
float length = 32;
float depth = 5;
SimpleBindings simpleBindings2 = new SimpleBindings();
simpleBindings2.put("globalWidth", width);
simpleBindings2.put("globalLength", length);
simpleBindings2.put("globalDepth", depth);
nashorn.eval("function gallons(width, length,
avgDepth){var volume = avgDepth * width * length; "+
" return volume * 7.48; } " +
"print(gallons(globalWidth, globalLength,
globalDepth));", simpleBindings2);
Result:
19148.800000000003
How It Works
To pass Java field values to JavaScript, use the
javax.script.SimpleBindings construct, which is basically a HashMap that
can be used for binding and passing values to the ScriptEngineManager . When
values are passed to the Nashorn engine in this manner, they can be accessed as global
variables within the JavaScript engine.
18-5.
Passing
Return
Values
from
JavaScript to Java
Problem
Search WWH ::




Custom Search