[英]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.