WebView没有重新加载具有相同URL的页面 [英] WebView not reloading page with identical URL

查看:137
本文介绍了WebView没有重新加载具有相同URL的页面的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

JavaFX在加载与之前已加载的页面具有相同URL的页面时显示一些奇怪的行为。下面的代码演示了这个问题:

JavaFX shows some odd behaviour when loading a page that has an identical url to a page that has been loaded before. The code below demonstrates this issue:

在加载一个页面的类初始化之后,使用自定义样式类将高亮显示应用于任意html元素。正确呈现此突出显示。

After class initalization a page is loaded, then a highlight is applied to an arbitrary html element using a custom style class. This highlight is rendered correctly.

最后,通过用户输入事件,WebView被告知要加载新页面(具有相同的URI)。而不是按原样显示页面,也会显示突出显示。

Finally, through user input event, the WebView is told to load a new page (with the same URI). Instead of showing the page as-is, the highlight is shown as well.

WebView webView = new WebView();
static String URI = "http://www.example.com";

public void loadPage() {
    // Step 1: load page
    webView.getEngine().load(URI);

    // Step 2: Change style attribute in page
    (Element) element = xpath.evaluate("//div[@id='mydiv']", webView.getEngine().getDocument(), XPathConstants.NODE);
    element.setAttribute("class", "mystyle");
}

handle() {
    // Step 3: load page again
    webView.getEngine().load(URI);
}

我已尝试使用WebView.getEngine()强制重新加载页面。 reload(),禁用缓存,等待工人完成等等。

I have experimented with forcing the page to reload with WebView.getEngine().reload(), disabling cache, waiting for workers to complete, etc.

我目前看到的唯一选项是创建一个新的WebView实例,但因为这是相当重CPU,我更喜欢重复使用对象,而不是每次想要恢复到原始页面时都重新创建它。

The only option I currently see is to create a new instance of the WebView, but as this is pretty CPU heavy, I prefer to reuse the object rather than creating it new every time I want to revert to the original page.

推荐答案

这是一个SSCCE,演示了HTML内容的重新加载。它与您的方法略有不同,但故事是相同的,但没有尝试加载像您这样的外部URL。你对webEngine的缓存是对的,因为 webEngine.reload()没有加载原始内容。

Here is a SSCCE that demonstrates the reloading of the HTML content. It is bit different from your approach but the story is the same, though didn't try loading an external URL like yours. You are right about caching of webEngine since the webEngine.reload() does not load the original content.

public class WebViewReload extends Application {

    private String content = "<html>"
            + "    <head>"
            + "         <style type=\"text/css\">"
            + "            .mystyle {"
            + "                padding: 20px;"
            + "                background-color: red;"
            + "                font-size: 30px;"
            + "            }"
            + "        </style>"
            + "    </head>"
            + "    <body>"
            + "        <div id=\"mydiv\">initial content</div>"
            + "    </body>"
            + "</html>";

    @Override
    public void start(final Stage stage) throws Exception {
        final WebView webView = new WebView();
        webView.getEngine().loadContent(content);
        // It is same as loading an external html source, like this
        // webView.getEngine().load(getClass().getResource("my.html").toExternalForm());

        Button btn1 = new Button("Apply style");
        btn1.setPrefWidth(200);
        btn1.setOnAction(new EventHandler<ActionEvent>() {
            @Override
            public void handle(ActionEvent event) {
                Element element = webView.getEngine().getDocument().getElementById("mydiv");
                element.setAttribute("class", "mystyle");
                element.setTextContent("new content");
            }
        });

        Button btn2 = new Button("Reload content");
        btn2.setPrefWidth(200);
        btn2.setOnAction(new EventHandler<ActionEvent>() {
            @Override
            public void handle(ActionEvent event) {
                webView.getEngine().loadContent(content);
                // following does not reload at all
                // webView.getEngine().reload();
            }
        });

        VBox vbox = new VBox(10);
        vbox.setPadding(new Insets(20));
        vbox.setStyle("-fx-background-color: gray");
        vbox.getChildren().addAll(webView, btn1, btn2);

        Scene scene = new Scene(vbox);
        stage.setScene(scene);
        stage.show();
    }

    public static void main(String[] args) {
        launch(args);
    }
}

这篇关于WebView没有重新加载具有相同URL的页面的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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