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, 2081🔥, 0💬
Popular Posts:
How to download and install Apache XMLBeans Source Package? The source package contains Java source ...
Java Architecture for XML Binding (JAXB) is a Java API that allows Java developers to map Java class...
JAX-WS is an API for building web services and clients. It is the next generation Web Services API r...
SLF4J API is a simple API that allows to plug in any desired logging library at deployment time. Her...
What Is poi-3.5.jar - Part 2? poi-3.5.jar is one of the JAR files for Apache POI 3.5, which provides...