Categories:
Audio (13)
Biotech (29)
Bytecode (36)
Database (77)
Framework (7)
Game (7)
General (507)
Graphics (53)
I/O (35)
IDE (2)
JAR Tools (102)
JavaBeans (21)
JDBC (121)
JDK (426)
JSP (20)
Logging (108)
Mail (58)
Messaging (8)
Network (84)
PDF (97)
Report (7)
Scripting (84)
Security (32)
Server (121)
Servlet (26)
SOAP (24)
Testing (54)
Web (15)
XML (322)
Collections:
Other Resources:
RhinoPoundToKilo.java - Exchange Data with Rhino Variables
How to exchange data between Java objects and Rhino JavaScript variables?
✍: FYIcenter
Exchanging data between Java objects and Rhino JavaScript variables
can be done in 2 ways:
1. Sending data from Java to JavaScript - Call Context.javaToJS() method first to convert a Java object to a Rhino object. Then call ScriptableObject.putProperty() method to assign the Rhino object to JavaScript variable:
Double pound = Double.valueOf(args[0]); Object jsP = Context.javaToJS(pound, s); ScriptableObject.putProperty(s, "p", jsP);
2. Sending data from JavaScript to Java - Call ScriptableObject.setProperty() method first to retrieve the JavaScript variable as a Rhino object. Then call Context.jsToJava() to convert the Rhino object to a Java object:
Object jsK = ScriptableObject.getProperty(s, "k"); Double kilo = (Double) Context.jsToJava(jsK, Double.class);
There is also a quick way to retrieve JavaScript variable by calling s.get() method:
Object jsK = s.get("k", s); double kilo = Context.toNumber(jsK);
Here is the entire example program, RhinoPoundToKilo, that shows how to exchange data between Java objects and Rhino JavaScript variables:
// Copyright (c) 2017 FYIcenter.com import org.mozilla.javascript.ContextFactory; import org.mozilla.javascript.Context; import org.mozilla.javascript.Scriptable; import org.mozilla.javascript.ScriptableObject; public class RhinoPoundToKilo { public static void main(String[] args) throws Exception { ContextFactory f = new ContextFactory(); Context c = f.enterContext(); Scriptable s = c.initStandardObjects(); String js = "function p2k(p) {\n"; js += " return p*0.453592;\n"; js += "}\n"; js += "var k = p2k(p);"; Double pound = Double.valueOf(args[0]); Object jsP = Context.javaToJS(pound, s); ScriptableObject.putProperty(s, "p", jsP); c.evaluateString(s, js, null, 1, null); Object jsK = ScriptableObject.getProperty(s, "k"); Double kilo = (Double) Context.jsToJava(jsK, Double.class); System.out.println("Pound to Kilo conversion:"); System.out.println(" Pound = "+pound); System.out.println(" Kilo = "+kilo); System.out.println("JavaScript used:"); System.out.println(js); } }
Compile and run the example program, RhinoPoundToKilo.java:
>\fyicenter\jdk-1.8.0\bin\javac -cp \fyicenter\rhino1_7R5\js.jar RhinoPoundToKilo.java >\fyicenter\jdk-1.8.0\bin\java -cp .;\fyicenter\rhino1_7R5\js.jar RhinoPoundToKilo 5.0 Pound to Kilo conversion: Pound = 5.0 Kilo = 2.26796 JavaScript used: function p2k(p) { return p*0.453592; } var k = p2k(p);
⇒ RhinoFunctionCall.java - Call JavaScript Function from Java
⇐ RhinoExportVar.java - Exporting Rhino Variable to Java
2017-08-08, ∼2226🔥, 0💬
Popular Posts:
commons-collections4-4.2 -sources.jaris the source JAR file for Apache Commons Collections 4.2, whic...
JDK 17 jdk.jdi.jmod is the JMOD file for JDK 17 JDI (Java Debug Interface) tool. JDK 17 JDI tool com...
JDK 11 jdk.scripting.nashorn.jm odis the JMOD file for JDK 11 Scripting Nashorn module. JDK 11 Scrip...
JDK 11 java.xml.crypto.jmod is the JMOD file for JDK 11 XML (eXtensible Markup Language) Crypto modu...
What Is poi-scratchpad-3.5.jar? poi-scratchpad-3.5.jar is one of the JAR files for Apache POI 3.5, w...