我在javafx中创建了一个Web视图,并在其中加载了一个网页。网页包含从服务器随机生成的图像。一旦页面加载我尝试运行JavaScript以获取图像的网址并尝试将其应用于图像查看器,但网址向服务器发出另一个请求另一个图像,它呈现不同的图像,但我想要相同的图像,这是先在webview上加载。我不希望我的网络浏览器应该请求另一个图像并进行渲染。
First.fxml
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.image.*?>
<?import javafx.scene.web.*?>
<?import java.lang.*?>
<?import java.util.*?>
<?import javafx.scene.*?>
<?import javafx.scene.control.*?>
<?import javafx.scene.layout.*?>
<AnchorPane id="AnchorPane" prefHeight="400.0" prefWidth="600.0" xmlns:fx="http://javafx.com/fxml/1" xmlns="http://javafx.com/javafx/8" fx:controller="com.jb.FirstController">
<children>
<SplitPane dividerPositions="0.3732638888888889" layoutX="10.0" layoutY="3.0" prefHeight="372.0" prefWidth="578.0">
<items>
<AnchorPane minHeight="0.0" minWidth="0.0" prefHeight="160.0" prefWidth="100.0">
<children>
<WebView fx:id="webview" layoutX="6.0" layoutY="10.0" prefHeight="346.0" prefWidth="200.0" />
</children>
</AnchorPane>
<AnchorPane minHeight="0.0" minWidth="0.0" prefHeight="160.0" prefWidth="100.0">
<children>
<ImageView fx:id="imageview" fitHeight="311.0" fitWidth="256.0" layoutX="23.0" layoutY="23.0" pickOnBounds="true" preserveRatio="true" />
</children>
</AnchorPane>
</items>
</SplitPane>
</children>
</AnchorPane>
FirstController.java
package com.jb;
import java.net.URL;
import java.util.ResourceBundle;
import javafx.beans.value.ChangeListener;
import javafx.beans.value.ObservableValue;
import javafx.concurrent.Worker;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.image.ImageViewBuilder;
import javafx.scene.web.WebEngine;
import javafx.scene.web.WebView;
/**
* FXML Controller class
*
* @author admin
*/
public class FirstController implements Initializable {
@FXML
WebView webview;
@FXML
ImageView imageview;
private WebEngine webengine;
@Override
public void initialize(URL url, ResourceBundle rb) {
webengine = webview.getEngine();
//once the page is loaded this event occures
webengine.getLoadWorker().stateProperty().addListener((obs,oldValue,newValue) -> {
//if page loaded successfully
if(newValue==Worker.State.SUCCEEDED)
{
//executing script to get the image src from webview
String img=(String)webengine.getDocument().getElementById("addimage").getAttribute("src");
System.out.println("img:"+img);
//img variable has the value 'http://localhost:8080/WebApplication10/getNewAddImage'
//'getNewAddImage' is a servlet which accept get request
//this servlet gives a random image from backend
//setting the url in image view
imageview.setImage(new Image(img));
}
});
//requesting to index.jsp to load in webview
webengine.load("http://localhost:8080/WebApplication10/index.jsp");
}
}
Test.java
package com.jb;
import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.layout.StackPane;
import javafx.stage.Stage;
/**
*
* @author admin
*/
public class Test extends Application {
@Override
public void start(Stage stage) throws Exception {
Parent root = FXMLLoader.load(getClass().getResource("First.fxml"));
Scene scene = new Scene(root);
stage.setScene(scene);
stage.show();
}
public static void main(String[] args) {
launch(args);
}
}
我需要加载首先在webview中加载的相同图像 我不会要求新图像
任何帮助将不胜感激
由于