如何从loadContent()加载html和javascript进入webengine? [英] How to load both html and javascript into webengine from loadContent()?

查看:856
本文介绍了如何从loadContent()加载html和javascript进入webengine?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

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");


推荐答案

我刚发现使用<$ c $ HTML中的< base> 标记也可以解决问题:

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

<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>

如果您通过 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.

这已经使用Java 8u25的 WebView 进行了测试。

This has been tested with WebView of Java 8u25.

这篇关于如何从loadContent()加载html和javascript进入webengine?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆