如何将 html 和 javascript 从 loadContent() 加载到 webengine 中?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20164062/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-12 23:37:38  来源:igfitidea点击:

How to load both html and javascript into webengine from loadContent()?

javawebjavafxcytoscape

提问by user2799603

Could someone provide some suggestions on how to load the following onto webviewer from loadContent()?

有人可以提供一些关于如何从 loadContent() 将以下内容加载到 webviewer 的建议吗?

http://jsbin.com/aqupun/6/edit

http://jsbin.com/aqupun/6/edit

I was trying to do something like this, but it doesn't seem to work. Thanks!

我试图做这样的事情,但它似乎不起作用。谢谢!

    Scanner sc1 = new Scanner(new File("src/web/web.html"));
    String webStr = sc1.useDelimiter("\Z").next();

    Scanner sc2 = new Scanner(new File("src/web/data.js"));
    String dataStr = sc2.useDelimiter("\Z").next();

    Scanner sc3 = new Scanner(new File("src/web/cytoscape.min.js"));
    String cytoStr = sc3.useDelimiter("\Z").next();

    Scanner sc4 = new Scanner(new File("src/web/jquery.min.js"));
    String jqueryStr = sc4.useDelimiter("\Z").next();

    webEngine.loadContent(cytoStr, "text/javascript");
    webEngine.loadContent(jqueryStr, "text/javascript");
    webEngine.loadContent(dataStr, "text/javascript");
    webEngine.loadContent(webStr, "text/html");

回答by Little Child

You just need to load your HTML page using the load()method of the WebEngine. The loading of the associated CSS and JavaScript will be done for you by the WebEngine.

你只需要使用加载HTML页面load()的方法WebEngine。相关 CSS 和 JavaScript 的加载将由WebEngine.

Here is how I have loaded AceEditorinto a WebView:

这是我如何加载AceEditor到一个WebView

enter image description here

在此处输入图片说明

and the code to do this was just of two lines:

执行此操作的代码只有两行:

engine = webView.getEngine();
engine.load("file:///home/littlejavachild/Downloads/AceEditor/ace-builds-master/MyTry.html");  

The loading of JavaScript source and CSS is handled for me by the engine.

JavaScript 源代码和 CSS 的加载由引擎为我处理。

The docs for the method is here:

该方法的文档在这里:

public void load(java.lang.String url)

Loads a Web page into this engine. This method starts asynchronous loading and returns immediately.

Parameters: url - URL of the web page to load

将网页加载到此引擎中。此方法启动异步加载并立即返回。

参数: url - 要加载的网页的 URL

回答by Andrey Chaschev

You first need to put these three files to the resources on the same level or on the hard drive.

首先需要把这三个文件放到同级资源或者硬盘上。

To load your content directly from memory you can use

要直接从内存中加载您的内容,您可以使用

webView.getEngine().loadContent("your html")

From JavaDoc:

JavaDoc

public void loadContent(String content)

Loads the given content directly. This method is useful when you have content composed in memory, or loaded from some system which cannot be reached via a URL.

public void loadContent(String content)

直接加载给定的内容。当您在内存中编写内容或从无法通过 URL 访问的某些系统加载内容时,此方法很有用。

Be aware though that the linked resources should be available by their urls, i.e. on disk or in resources. To reflect dynamic changes in your web app I suggest you to call Java from JS. This can be done by providing Java object into JS app: Communication between JavaFX and JavaScript inside WebView, using JSObject

请注意,链接的资源应该通过它们的 url 可用,即在磁盘上或资源中。为了反映 Web 应用程序中的动态变化,我建议您从 JS 调用 Java。这可以通过向 JS 应用程序提供 Java 对象来完成:WebView 中 JavaFX 和 JavaScript 之间的通信,使用 JSObject

Here you may find a browser demo and a simplified WebView component: Java GUI to display webpages and return HTML.

在这里你可以找到一个浏览器演示和一个简化的 WebView 组件:Java GUI to display pages and return HTML

回答by Huxi

I just found out that using the <base>tag in the HTML also does the trick:

我刚刚发现<base>在 HTML中使用标签也能解决问题:

<html>
    <head>
        <title>The slash at the end of the href is important!</title>
        <base href="file:///absolute/path/to/your/docroot/" />
    </head>
    <body>
        <img src="image.png"/>
    </body>
</html>

If you load the above code via engine.loadContent(String)then image.pngwill be loaded from /absolute/path/to/your/docroot/image.png.

如果您通过加载上面的代码,engine.loadContent(String)那么image.png将从/absolute/path/to/your/docroot/image.png.

This method is easier if you need to load multiple resources since you only have to specify the absolute path at a single place.

如果您需要加载多个资源,则此方法更容易,因为您只需在一个位置指定绝对路径。

This has been tested with WebViewof Java 8u25.

这已经用WebViewJava 8u25进行了测试。