JavaScript 函数执行后如何防止页面重新加载?

时间:2021-03-03 23:39:08

标签: javascript java jsf primefaces

我正在用 Java 开发一个 Web 应用程序。但出于某些目的,我必须使用 javascript。我正面临一个不定式循环问题,原因是我相信非常相同的 javascript。 我通过单击命令链接触发 java bean 中的 init 方法。这个命令链接应该打开一个新的 XHTML 页面。在初始化期间,我必须解析进一步实现所需的本地 IP 地址。我在 Stack Over Flow 上找到了 javascript,它将为我解决这个问题。执行javascript后,我需要将该IP存储到某个变量中并将其传递回bean。我找到了一种使用 的方法,我直接从 javascript 本身触发它。我设法成功地将变量传递给 bean,并将其用于其他功能。在这一点上,我面临一个问题,我不知道如何解决。脚本以某种方式成功执行后,页面会重新加载,并再次调用 init 方法。所以它基本上创建了一个无限循环。

这是我在 java bean 中的 init 方法:

public void init() {

        RequestContext.getCurrentInstance()
        .execute("getLocalIP();");
}

用于解析本地 IP 地址的 JavaScript:

 window.RTCPeerConnection = window.RTCPeerConnection || window.mozRTCPeerConnection || window.webkitRTCPeerConnection;
    
    var pc = new RTCPeerConnection({
        iceServers : []
        }), noop = function() {};
    
    pc.createDataChannel('');
    pc.createOffer(pc.setLocalDescription.bind(pc), noop);
    pc.onicecandidate = function(ice) {
        if (ice && ice.candidate && ice.candidate.candidate) {
            var localIP = /([0-9]{1,3}(\.[0-9]{1,3}){3}|[a-f0-9]{1,4}(:[a-f0-9]{1,4}) 
                      {7})/.exec(ice.candidate.candidate)[1];
            console.log('my IP: ', localIP);
            
            // this will trigger the remoteCommand 
            // which will pass value from JS to java bean
            loadLocalIP([{name:'localIPParam', value: localIP}]);
            pc.onicecandidate = noop;
        }
    }

这是我的 xhtml(remoteCommand),它是从 javascript 触发的:

 <h:form id="localIPForm">
        <p:remoteCommand name="loadLocalIP" action="#{aggregationStationController.loadData()}"/>
 </h:form>

这个方法由remoteCommand调用:

 public void loadData() {

    Map<String, String> params = FacesContext.getCurrentInstance()
                                             .getExternalContext()
                                             .getRequestParameterMap();
    ipValue = params.get("localIPParam").toString();

    System.out.println(ipValue);

    if (!ipValue.isEmpty()) {
        resolveAggregationStationByIP(ipValue);
    }
 // after this line init method is invoked again and creates an infinite loop
 }

 private void resolveAggregationStationByIP(String ipAddress) {

    aggregationStation = organizationStructureService.getOrgEntityByTypesAndAttrCodeAndAttrValue(
            Arrays.asList(OrgEntityTypeCode.AGGREGATION_STATION.getCode(), 
            OrgEntityTypeCode.PROD_LINE.getCode()), "IP", ipAddress);

    if (aggregationStation != null) {
        printerName = organizationStructureService.
                      getOrgEntityAttrValue(aggregationStation, "PRINTER_NAME");

        if (printerName == null) {
            printerName = "";
        }

        // check if there is order in progress for station
        selectedPackagingOrder = getInProgressOrderForStation();

        loadOperationTypes();

        populateDataFromDB();
    } else {
        error = true;
        errorMessage = MessageUtil.interpolate("ip_not_registred_as_station", ipAddress, 
                       MessageUtil.interpolate(OrgEntityTypeCode.AGGREGATION_STATION.getCode()));
    }
}

我的问题是:如何避免多次执行 javascript?

如果能帮我解决这个问题,我将不胜感激。提前致谢!

1 个答案:

答案 0 :(得分:1)

尝试使用 event.preventDefault() 来防止页面刷新。