java jsobject,获得一个JAVAOBJECT了JSObject的

"本文探讨了如何在JavaScript中通过HTML元素获取数据,并将其转换为Java对象,特别是Byte数组。作者遇到的问题是尝试从JSObject获取Java对象时返回了空值。尝试了修改JavaScript代码以获取元素的value属性,但只得到了字符串表示而非实际对象。解决方案在于正确地使用JSObject的`getMember("value")`方法来获取Java对象。"
摘要由CSDN通过智能技术生成

I have a Javascript function in my xhtml page that does the following:

HTML FILE:

function getData(){

var data = document.getElementById('data');

return data;

}

Backing Bean Code:

public class Bean{

public byte[] getBytes(){

return this.bytes;

}

}

And I have an applet that needs to get this byte array from the html

Applet code:

public class TestApplet extends Applet{

JSObject win = JSObject.getWindow(this);

JSObject returnedValue = win.call("getData", null);

}

I've been trying to call the returnedValue.getMember("value") (fixed); but that gets a null value.

I also tried to change the javascript to this:

HTML:

function getData(){

var data = document.getElementById('data').value;

return data;

}

But that will only return me the String representation of the byte[], not the actual object.

So my question is:

How do I use JSObject to get a JavaObject?

Current method gets me back a String

解决方案

There is no data property.

Change it to returnedValue.getMember("value").

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
当然,我可以帮你解决这个问题。你可以使用JavaFX内置的Web引擎来加载网页并执行JavaScript代码。以下是一个简单的示例代码,可以加载一个包含JavaScript代码的网页并执行其中的函数: ``` import javafx.application.Application; import javafx.scene.Scene; import javafx.scene.web.WebEngine; import javafx.scene.web.WebView; import javafx.stage.Stage; public class Main extends Application { @Override public void start(Stage primaryStage) { WebView webView = new WebView(); WebEngine webEngine = webView.getEngine(); webEngine.load("https://www.example.com/myscript.html"); // Wait for the web page to finish loading webEngine.getLoadWorker().stateProperty().addListener((obs, oldState, newState) -> { if (newState == Worker.State.SUCCEEDED) { JSObject window = (JSObject) webEngine.executeScript("window"); // Call the JavaScript function named "myFunction" with the argument "Hello, world!" window.call("myFunction", "Hello, world!"); } }); Scene scene = new Scene(webView, 800, 600); primaryStage.setScene(scene); primaryStage.show(); } public static void main(String[] args) { launch(args); } } ``` 在这个示例中,我们创建了一个WebView并加载了一个包含JavaScript代码的网页。当网页加载完成后,我们使用WebEngine的executeScript()方法来执行JavaScript代码。在这个例子中,我们调用了一个名为“myFunction”的函数,并向其传递了一个字符串参数“Hello, world!”。 希望这可以帮助你实现你的目标。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值