Fxml文件
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.layout.BorderPane?>
<?import javafx.scene.web.WebView?>
<BorderPane fx:id="borderPane" xmlns:fx="http://javafx.com/fxml" fx:controller="com.action.layout.MainAction">
<center>
<WebView fx:id="centerWebView" ></WebView>
</center>
</BorderPane>
Action文件
package com.action.layout;
import java.net.URL;
import java.util.ResourceBundle;
import javafx.beans.value.ObservableValue;
import javafx.concurrent.Worker.State;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.layout.BorderPane;
import javafx.scene.web.WebEngine;
import javafx.scene.web.WebEvent;
import javafx.scene.web.WebView;
import netscape.javascript.JSObject;
public class MainAction implements Initializable {
@FXML BorderPane borderPane;
@FXML WebView centerWebView;
@Override
public void initialize(URL location, ResourceBundle resources) {
center();
}
public void center() {
//获取Engine
final WebEngine webEngine = centerWebView.getEngine();
//获取当前Java版本
System.out.println("Java Version:"+System.getProperty("java.runtime.version"));
//获取当前JavaFx版本
System.out.println("JavaFXersion:"+System.getProperty("javafx.runtime.version"));
//获取当前系统版本
System.out.println("OS:"+System.getProperty("os.name")+","+System.getProperty("os.arch"))
//获取WebKit内核版本
System.out.println("User Agent:"+centerWebView.getEngine().getUserAgent());
//获取页面路径
String mainUrl = getClass().getResource("/pages/layout/index.html").toExternalForm();
//加载方法接口
webEngine.getLoadWorker().stateProperty().addListener((ObservableValue<? extends State> ov, State oldState, State newState) -> {
//判断页面是否加载完毕
if (newState == State.SUCCEEDED) {
JSObject win = (JSObject) webEngine.executeScript("window");
win.setMember("employeeAction", employeeAction); //人员
}
});
//获取页面alert弹窗和重新加载页面
centerWebView.getEngine().setOnAlert((WebEvent<String> wEvent) -> {
//打印页面弹窗
System.out.println("Alert Event - Message: " + wEvent.getData());
if(wEvent.getData().equals("Awesome123123")) {
//重新加载页面
webEngine.load(getClass().getResource("/pages/layout/test.html").toExternalForm());
}
if(wEvent.getData().equals("123")) {
webEngine.load(getClass().getResource("/pages/layout/demo.html").toExternalForm());
}
});
//初始化
webEngine.load(mainUrl);
}
}
前台页面
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<script type='text/javascript' src="jquery-1.12.1.min.js"></script>
<script type='text/javascript' src="jquery-1.7.1.js"></script>
<script type='text/javascript' src="json2.js"></script>
<!-- Visibility toggle script -->
<script type="text/javascript">
function check(userName,password){
var result = employeeAction.checkPassword(userName,password);
document.getElementById("checkResult").value=result;
}
function updatePassword(){
var pass = document.getElementById("password").value;
document.getElementById("checkResult").value=pass;
var a = employeeAction.resetPassword("root",pass);
document.getElementById("password").value=a;
}
function insert(){
var userName = document.getElementById("userName").value;
var password = document.getElementById("userPassword").value;
var a = employeeAction.insertEmployee(userName,password);
document.getElementById("password").value=a;
}
function query(){
var str = employeeAction.queryAll();
var jsondata = JSON.parse(str);
alert(jsondata);
document.getElementById("password").value=jsondata[0].userName;
}
</script>
</head>
<body>
<p>Employee</p>
<input type="text" value="" id="checkResult">
<input type="button" value="验证密码" onclick="check('root','root');">
<input type="text" value="" id="password">
<input type="button" value="重置" onclick="updatePassword();">
<input type="text" value="" id="userName">
<input type="text" value="" id="userPassword">
<input type="button" value="添加" onclick="insert();">
<br>
<input type="button" value="查询" onclick="query();">
</body>
</html>
仅供参考