WebView组件不提供开箱即用的Web资源缓存。但是,它确实利用java.net堆栈进行网络通信。这意味着您可以安装自己的URL处理程序,该处理程序与缓存进行对话并从缓存中提供资源。例如,把这样的事情块你main()
方法JavaFX的推出呼叫前:
URL.setURLStreamHandlerFactory(new URLStreamHandlerFactory() {
public URLStreamHandler createURLStreamHandler(String protocol) {
if ("http".equals(protocol)) {
return new URLStreamHandler() {
protected URLConnection openConnection(URL u) throws IOException {
if (isCached(u)) {
return new CachedStreamConnection(u);
}
return new MyURLConnection(u);
}
};
}
// Don't handle a non-http protocol, so just return null and let
// the system return the default one.
return null;
}
});
当然,魔鬼存在于细节之中。当将缓存中的资源存储到缓存中时,您应该考虑由HTTP头返回的缓存策略(如ETags),因此您应该使用应该使用。另一个考虑因素是HTML元标记。 Here是一个很好的缓存资源。
您可能还想考虑一个cookie管理系统来补充这个缓存管理系统。