JavaFX WebEngine上的JSP

时间:2016-08-02 07:17:26

标签: jsp javafx javafx-webengine

我需要从json文件中读取信息,该文件位于user.home目录Windows 7中。为此,我想使用javascript。但Javascript无法通过java webEngine访问本地驱动器。为此,我使用java Class从json文件中读取信息,并使用jsp显示结果。

Utils.java :

public class AdminUtils{

  public String getBasStatus() {
        try {
            String userHomePath = System.getProperty("user.home");
            userHomePath = userHomePath + File.separator + "MY_PROJECT_FOLDER";
            String fileName = userHomePath + File.separator + "deviceStatus.json";
            File jsonFile = new File(fileName);
            String jsonString = null;
            jsonString = FileUtils.readFileToString(jsonFile);
            JsonElement element = new JsonParser().parse(jsonString);
            JsonObject jsonObject = element.getAsJsonObject();
            return jsonObject.get("billAcceptorStatus").toString();
        } catch (IOException e) {
            e.printStackTrace();
            return "";
        }
    }

    public String getPadStatus(){

        try {
            String userHomePath = System.getProperty("user.home");
            userHomePath = userHomePath + File.separator + "MY_PROJECT_FOLDER";
            String fileName = userHomePath + File.separator + "deviceStatus.json";
            File jsonFile = new File(fileName);
            String jsonString = null;
            jsonString = FileUtils.readFileToString(jsonFile);
            JsonElement element = new JsonParser().parse(jsonString);
            JsonObject jsonObject = element.getAsJsonObject();
            return jsonObject.get("pinPadStatus").toString();
        } catch (IOException e) {
            e.printStackTrace();
            return "";
        }

    }
}

这是deviceStatus.jsp

<%@ page import="util.AdminUtils" %>
<!DOCTYPE html>
<html lang="en">
<head>
    <meta http-equiv="Cache-Control" content="no-store"/>
    <meta http-equiv="Pragma" content="no-cache"/>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
    <meta http-equiv="X-UA-Compatible" content="IE=edge"/>
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Device Status</title>
    <style>
        .defaultStatus {
            width: 50px;
            height: 50px;
            display: block;
            background: url("./../images/deviceStatusIcons/question.png") no-repeat;
            background-size: contain;
        }

        .goodStatus {
            width: 50px;
            height: 50px;
            display: block;
            background: url("./../images/deviceStatusIcons/check.png") no-repeat;
            background-size: contain;
        }

        .badStatus {
            width: 50px;
            height: 50px;
            display: block;
            background: url("./../images/deviceStatusIcons/delete.png") no-repeat;
            background-size: contain;
        }
    </style>
    <script>
      <%
        AdminUtils utils = new AdminUtils("username", "password");
      %>
        var basStatus = <%=utils.getBasStatus()%> ;
        var padStatus = <%=utils.getPadStatus()%> ;

        function billAcceptorStatus() {
            if (basStatus == 0) {
                $("#billAcceptorStatus").attr('class', 'goodStatus');
            }  else if (basStatus == 2) {
                $("#billAcceptorStatus").attr('class', 'badStatus');
            } else {
                $("#billAcceptorStatus").attr('class', 'defaultStatus');
            }
        }
        function pinPadStatus() {
            if (padStatus == 0) {
                $("#pinPadStatus").attr('class', 'goodStatus');
            } else if (padStatus == 2) {
                $("#pinPadStatus").attr('class', 'badStatus');
            } else {
                $("#pinPadStatus").attr('class', 'defaultStatus');
            }
        }
    </script>
</head>
<body>
<div class="content">
    <div class="row">
        <div style="width:100%;height:150px;text-align: center;">
            <h2>DEVICE STATUS PAGE</h2>
        </div>
        <div style="width: 100%;height: 600px;">
            <table style="width: 60%;border-collapse:collapse;" align="center">
                <caption>DEVICE STATUSES</caption>
                <tr style="border-bottom: 1px solid #adadad; background-color: #adadad">
                    <td width="10%">№</td>
                    <td width="20%">Name</td>
                    <td width="20%">Status</td>
                </tr>

                <tr style="border-bottom: 1px solid #adadad;border-top: 1px solid #adadad;  background-color: gainsboro">
                    <td width="10%">1</td>
                    <td width="20%">
                        <p>Pin Pad</p>
                    </td>
                    <td width="20%">
                        <span id="pinPadStatus" class="defaultStatus"></span>
                    </td>
                </tr>

                <tr style="border-bottom: 1px solid #adadad;border-top: 1px solid #adadad;  background-color: gainsboro">
                    <td width="10%">2</td>
                    <td width="20%">
                        <p>Bill Acceptor</p>
                    </td>
                    <td width="20%">
                        <span id="billAcceptorStatus" class="defaultStatus"></span>
                    </td>
                </tr>

            </table>
            <br/><br/>
        </div>
    </div>
</div>
</body>
</html>

这是一个deviceStatus.json文件:

{"billAcceptorStatus":"-1","pinPadStatus":"-1"}

这是将代码加载到deviceStatus.jsp到webengine:

   if (Objects.equals(adminPassword.get(1), "4")) {
            if (Objects.equals(adminPassword.get(2), "5")) {
                if (Objects.equals(adminPassword.get(3), "6")) {
                    adminPassword.clear();
                    webEngine.load(AtmApplication.class.getResource("/html/deviceStatus.jsp").toExternalForm());
                    adminMode = false;
                } else loadDefaultPage();
            } else loadDefaultPage();
        }

但是jsp文件没有加载到webEngine。如果一起jsp文件 - 加载html文件,webengine加载html文件。但我需要加载jsp文件。

1 个答案:

答案 0 :(得分:0)

JSP是服务器端技术,而不是客户端技术。它由服务器转换为HTML,以便浏览器可以显示它。 JavaFX是一种客户端技术,与Web浏览器一样,Web引擎只能处理HTML文件,而不能处理JSP文件。