简体   繁体   English

如何从javafx.scene.web.WebEngine #loadContent加载的html页面获取css和图像文件?

[英]How to reach css and image files from the html page loaded by javafx.scene.web.WebEngine#loadContent?

I have a String HTML content which is loaded into webEngine by loadContent() method. 我有一个String HTML内容,通过loadContent()方法加载到webEngine I have also some css and image files used in this page. 我还有一些在此页面中使用的CSS和图像文件。 Although I put these file into the same package of java class, the loaded page cannot find them. 虽然我把这些文件放在java类的同一个包中,但是加载的页面找不到它们。 Looked for API docs and web, but could not find any appropiate similar solutions. 寻找API文档和Web,但找不到任何适合的类似解决方案。 How I load these files? 我如何加载这些文件?

You can place your string html content in a file in the same package as the Java class and use the engine.load(String url) method instead: 您可以将字符串html内容放在与Java类相同的包中的文件中,并使用engine.load(String url)方法:

engine.load(getClass().getResource("mypage.html").toExternalForm());

When you do this, all relative links in the html page will resolve to resources (eg css and image files) in your Java package. 执行此操作时,html页面中的所有相对链接将解析为Java包中的资源(例如css和图像文件)。

Beware that if you are loading a resource that is located in a jar file, that the jar: protocol does not understand relative links with parent specifiers. 请注意,如果要加载位于jar文件中的资源,则jar:协议无法理解与父指定符的相对链接。 Eg, <img src="../images/image.png"/> will not work, but <img src="/images/image.png"/> or <img src="images/image.png"/> will as long (as you put the image in the appropriate location in the jar file). 例如, <img src="../images/image.png"/>将无效,但<img src="/images/image.png"/><img src="images/image.png"/>将一样长(因为您将图像放在jar文件中的适当位置)。 The file: protocol does not have such restrictions and .. relative links will work fine when the resources are loaded using it. file:协议中没有这样的限制和..当资源都用它装相对链接将正常工作。

If the html string is dynamically generated by your java code rather than static, then Sergey's solution is probably best. 如果html字符串是由你的java代码而不是静态生成的,那么谢尔盖的解决方案可能是最好的。

You need to set your local paths in html string for loadContent next way: 您需要在html字符串中为loadContent设置本地路径:

view.getEngine().loadContent(
"<img src='" + getClass().getResource("1.jpg") + "' />");

I just found out that using the <base> tag in the HTML also does the trick: 我刚刚发现在HTML中使用<base>标签也可以解决问题:

<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.png will 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 WebView of Java 8u25. 这已经使用Java 8u25的WebView进行了测试。

试试这个

<link href="file:css\default.css" rel="stylesheet" type="text/css" />

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何从loadContent()加载html和javascript进入webengine? - How to load both html and javascript into webengine from loadContent()? JavaFX 2.0+ WebView / WebEngine将网页呈现给图像 - JavaFX 2.0+ WebView /WebEngine render web page to an image Javafx WebEngine:背景工作者到底发生了什么? UI挂起在loadContent(大HTML文档) - Javafx WebEngine: what really happens in a background worker? UI hangs on loadContent(big HTML document) 如何单击加载到JavaFX Webengine的网站上的按钮 - How to click a button on website loaded into JavaFX Webengine WebEngine 未加载以 HTML (javafx) 链接的文件 - WebEngine is not loading files linked in HTML (javafx) 如何通过 JavaFX WebEngine 通过 CSS 类名获取 HTML 元素? - How do I get an HTML Element By CSS class name via a JavaFX WebEngine? JavaFx WebEngine中的HTML下拉框 - Html dropdown box in javaFx webEngine 如何在WebEngine中从javascript调用JavaFX的UI变量? - How to Call to a UI variable of JavaFX from javascript in WebEngine? 代号一个错误java.lang.NoSuchMethodError:javafx.scene.web.WebEngine.setUserDataDirectory(Ljava / io / File;)V - codename one error java.lang.NoSuchMethodError: javafx.scene.web.WebEngine.setUserDataDirectory(Ljava/io/File;)V 如何提高JavaFX中的场景快照所产生的图像的DPI质量 - How to better the DPI quality of the image resulting from a scene scrennshot in JavaFX
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM