[英]JavaFX 2.2 bidirectional communication in javascript and webview
I am working on JavaFX 2.2, I am using webview to integrate html code in the JavaFX scene and was able to load the html correctly. 我正在使用JavaFX 2.2,正在使用webview在JavaFX场景中集成html代码,并且能够正确加载html。 I was following this link and was trying to pass some objects from javascript to webview/controller but I am getting null values at java side.
我正在跟踪此链接,并试图将一些对象从javascript传递到webview / controller,但在Java端却得到了空值。
I have saved a interface object in JSObject like below 我已经在JSObject中保存了一个接口对象,如下所示
webEngine.getLoadWorker().stateProperty().addListener(
new ChangeListener<State>() {
@Override
public void changed(ObservableValue<? extends State> ov,
State oldState, State newState) {
if (newState == State.SUCCEEDED) {
JSObject win = (JSObject) webEngine.executeScript("window");
win.setMember("app", new JavaApp());
}
}
}
);
I have created a class 我创建了一个班
public class JavaApp {
public void exit() {
Platform.exit();
}
public void print(Date date) {
System.out.println("Parm:"+date);
}
public Date getValue() {
return new Date();
}
}
My html is 我的HTML是
<html lang="en">
<head>
<script type="text/javascript">
function callJava(){
app.print(new Date());
var val = app.getValue();
app.print(val);
}
</script>
</head>
<body>
<p>Help</p>
<p><a href="about:blank" onclick="callJava();">Exit the Application</a></p>
</body>
</html>
In above code I always get null values printed in JavaApp.print() method. 在上面的代码中,我总是在JavaApp.print()方法中打印出空值。 Interesting point is when I changed the parameter from Date to String in print method and pass the string from javascript, I get correct values.
有趣的一点是,当我在打印方法中将参数从日期更改为字符串并从javascript传递字符串时,我得到了正确的值。
How I can transfer objects in this case especially Date object. 在这种情况下,我如何传输对象,尤其是Date对象。 Any help is much appritiated
任何帮助都值得
Java does not know the Date
class of javascript . Java不知道javascript的
Date
类。
2.3 Data Type Conversions 2.3数据类型转换
You can pass a javascript object to Java , ( you have to wrap it in a JSObject
) but you can get directly only parameters like these: 您可以将javascript对象传递给Java ,(必须将其包装在
JSObject
),但是您只能直接获取如下参数:
My solution for you: 我为您提供的解决方案:
public class JavaApp {
public void exit() {
Platform.exit();
}
//public void print(Date date) {
// System.out.println("Parm:"+date);
// }
public void print(long date) {
System.out.println("Parm:"+new Date(date));
}
public Date getValue() {
return new Date();
}
}
<html lang="en">
<head>
<script type="text/javascript">
function callJava(){
// app.print(new Date());
app.print(new Date().getTime());
var val = app.getValue();
app.print(val);
}
</script>
</head>
<body>
<p>Help</p>
<p><a href="about:blank" onclick="callJava();">Exit the Application</a></p>
</body>
</html>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.