简体   繁体   English

WebView不显示任何内容

[英]WebView not showing anything

I have a pretty simple application that lists rows from a database in a tableview. 我有一个非常简单的应用程序,该应用程序在tableview中列出了数据库中的行。 When the user clicks on a row in that list, the application grabs the XML column from that row, and is supposed to display it in a WebView in the same window. 当用户单击该列表中的一行时,应用程序将从该行中获取XML列,并应将其显示在同一窗口的WebView中。 Everything other than actually displaying the XML works fine. 除了实际显示XML外,其他所有方法都可以正常工作。 I've been beating my head on this for a while, but I'm not getting anywhere. 我已经为此花了好一阵子,但是我什么也没得到。 Here's the code that the listener calls: 这是侦听器调用的代码:

    @FXML
    private void showXML(QueryRow row) {

        String msg = "";
        try {
            msg = mainApp.getMsg(row.getID().get());
        } catch (SQLException e) {
            e.printStackTrace();
        }
        final String fm = msg;

        System.out.println(msg);

        //tb.setText(msg);

        webEngine = webView.getEngine();
//      webEngine.getLoadWorker().stateProperty().addListener(new ChangeListener<State>() {
//        
//          public void changed(ObservableValue ov, State oldState, State newState) {
//
//            if (newState == Worker.State.SUCCEEDED) {
//              System.out.println("inside");
//              webEngine.load(fm);
//              //stage.setTitle(webEngine.getLocation());
//            }
//
//          }
//        });
        System.out.println("Go baby go!");
        webEngine.load(fm);

    }

What am I missing? 我想念什么?

If you want to load XML and fm is not link then you should probably use 如果您想加载XML并且fm不是链接,那么您应该使用

webEngine.loadContent(fm);

/**
 * Loads the given HTML content directly. This method is useful when you have an HTML
 * String composed in memory, or loaded from some system which cannot be reached via
 * a URL (for example, the HTML text may have come from a database). As with
 * {@link #load(String)}, this method is asynchronous.
 */
public void loadContent(String content) {
    loadContent(content, "text/html");
}

But this will not make xml visible, so if you want your xml to be displayed, you have to put it in some default html page. 但这不会使xml可见,因此,如果要显示xml,则必须将其放在一些默认的html页面中。 Something like this: https://gist.github.com/jewelsea/1463485 像这样的东西: https : //gist.github.com/jewelsea/1463485

or simple way: 或简单的方法:

webEngine.loadContent(
         <textarea readonly style='width:100%; height:100%'>"+ fm +"</textarea>")

Ok, I just tested a little bit. 好吧,我只是做了一点测试。 text/html is the correct way, but you need to do some work on your xml data: You have to escape the XML entities (I use commons-lang3 StringEscapeUtils for that) and then wrap all in a preformatted html string: text / html是正确的方法,但是您需要对xml数据做一些工作:必须转义XML实体(为此,我使用commons-lang3 StringEscapeUtils),然后将其包装在预格式化的html字符串中:

public class JavaFXTest extends Application {

    @Override
    public void start(Stage primaryStage) throws Exception {
        primaryStage.setTitle("Test to display XML");

        BorderPane content = new BorderPane();
        WebView webView = new WebView();
        WebEngine webEngine = webView.getEngine();

        String xml = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<tag1>\n  <tag2>hello</tag2>\n</tag1>";
        String escaped = StringEscapeUtils.escapeHtml4(xml);
        String html = "<html><head></head><body><pre>" + escaped + "</pre></body>";

        webEngine.loadContent(html, "text/html");

        content.setCenter(webView);
        primaryStage.setScene(new Scene(content, 400, 300));
        primaryStage.show();
    }

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

This produces the following window: 这将产生以下窗口:

在此处输入图片说明

Addition: You might need to do some pretty printing on the xml before escaping; 另外:在转义之前,您可能需要在xml上做一些漂亮的打印。 I just used hard coded line breaks and spaces. 我只是使用硬编码的换行符和空格。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM