javafx webview not supporting Ajax web features

不羁的心 提交于 2020-02-01 04:58:05

问题


I am trying to open webpage in webview using JavaFx . Its opening the web page properly but its not supporting the Ajax based web features like partial refreshing and new window popup handling I am using the following code

  final Group group= new Group();
  Scene scene= new Scene(group);
  fxpanel.setScene(scene);    
  WebView  webview = new WebView ();
  group.getChildren().add(webview);

  eng= webview.getEngine();
  eng.setJavaScriptEnabled(true);




    try{

            String url="http://www.abc.com";
            eng.load(url);
            eng.setCreatePopupHandler(
            new Callback<PopupFeatures, WebEngine>() {
            @Override
            public WebEngine call(PopupFeatures config) {
            smallView = new WebView();
            smallView.setFontScale(0.8);

            ChatPopup frm = new ChatPopup(smallView);
            frm.setBounds(0,0,400,250);
            frm.setVisible(true);
            return smallView.getEngine();

            }
        });





       }
  catch(Exception ex){}

             }

回答1:


WebView does support Ajax.

  1. Run the following app.
  2. Click on the "Load data from server into div" button.
  3. Page will be refreshed with data fetched from the server.

import javafx.application.Application;
import static javafx.application.Application.launch;
import javafx.scene.Scene;
import javafx.scene.web.WebView;
import javafx.stage.Stage;

public class WebViewAjax extends Application {
  public static void main(String[] args) { launch(args); }
  @Override public void start(Stage stage) {
    WebView webView = new WebView();
    webView.getEngine().load("http://www.jquerysample.com/#BasicAJAX");

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

Aside on alerts

Note, in the sample page linked above there there are numerous examples for handling the json data. Some of the examples, e.g. the jquery $.get() example, output the result of the ajax call using a JavaScript alert().

If you want to see the alert data, you need to add an alert handler to the WebView engine. A basic alert handler such as below will just output the alert data to the output console:

webView.getEngine().setOnAlert(
    stringWebEvent -> System.out.println(stringWebEvent.getData())
);

This is not really related to ajax calls, but without an alert handler, you may be confused you if you are trying to use an alert to debug or show data returned from a WebView ajax call.




回答2:


If you need to make AJAX calls to cross-site services in the WebView, you can get around the security restrictions by making your AJAX calls through an up-call to Java. For example you could write or find a class with a ".request()" method that takes a JSObject as a parameter (the same JSObject format that jQuery's $.ajax() method takes, preferably), and inject a Java object that will expose that method:

WebView myWebView; //assuming it's initialized and points to an actual WebView

WebEngine engine = myWebView.getEngine();
JSObject window = null;
try{
    window = (JSObject) engine.executeScript("window");
}catch (JSException e){
    e.printStackTrace();
}
if (window != null){
    window.setMember("myAjax", new AJAXProxyClass());
}

You can also directly override jQuery's ajax method with your own upcalling method, so that the difference is completely transparent to the javascript code, i.e.:

engine.executeScript("$.ajax = new function (o) { myAjax.request(o); };");
engine.executeScript("_$ = window.$");

This will replace jQuery's "$.ajax" call with the one from your Java object seamlessly. (I set the "_$" variable because jQuery will overwrite $ with it sometimes if it detects conflicts, returning jQuery to its original version.) This means that in most cases, any javascript code will not have to care whether it is running in your WebView or not.

A warning, though - jQuery's ajax call is rather complex, and this may break some of the jQuery ajax extensions if you're not careful about how you handle it. It should work just fine for the most common kinds of GET and POST calls, though.



来源:https://stackoverflow.com/questions/16392099/javafx-webview-not-supporting-ajax-web-features

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!