如何从javascript正确启动/停止PrimeFaces Ajax投票 [英] How to correctly start/stop a PrimeFaces Ajax Poll from javascript

查看:75
本文介绍了如何从javascript正确启动/停止PrimeFaces Ajax投票的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想要一个小的Web应用程序,用于测试与服务器的连接并返回状态消息.单击开始按钮后,我想每4秒轮询一次并返回一个状态文本.但是,如果我单击按钮,它将仅自动返回,而不会返回. 这是我的代码:

I want a little webapp for testing a connection to a server and returning a status message. After clicking a start button, I want to poll every 4 seconds and return a status text. But it will only return, if I click on the button, not automatically. Here is my code:

index.xhtml:

index.xhtml:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" 
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
    xmlns:h="http://java.sun.com/jsf/html"
    xmlns:f="http://java.sun.com/jsf/core"
    xmlns:ui="http://java.sun.com/jsf/facelets"
    xmlns:p="http://primefaces.org/ui">

<h:head>
</h:head>
    <h:body>
    <h1>My App</h1>
        <h:form onkeypress="poll.stop()">
            Server URL: <h:inputText id="name" value="#{helloBean.name}"></h:inputText>
            <p:commandButton value="Start Watching!" onclick="poll.start()" />

            <pre><h:outputText id="output" value="#{helloBean.playground}" /></pre>
            <p:poll interval="4" listener="#{helloBean.getPlayground}" update="output" widgetVar="poll" autoStart="false" />
        </h:form>
</h:body>
</html>

还有我的豆子

@ManagedBean
@SessionScoped
public class HelloBean implements Serializable {
        public String getPlayground(){
        if(name.length() < 3){
            return "";
        }
        DateFormat dateFormat = new SimpleDateFormat("dd-MMM-yyyy hh:mm:ss");
        Date date = new Date();
        String date1 = dateFormat.format(date);
        int code = 0;
        String urldns = null;
        try{
        URL url = new URL(name);
        HttpURLConnection connection = (HttpURLConnection)url.openConnection();
        connection.setRequestMethod("GET");
        connection.connect();
        code = connection.getResponseCode();
        urldns = connection.getURL().toString();
        connection.disconnect();
        }
        catch(Exception ex){
            name2.add(date1+" Uhr : "+name+" -> nicht erreichbar!");
        }

        if(code==200){
            if(urldns.contains("dnserror")){
                name2.add(date1+" Uhr : "+name+" -> nicht erreichbar!");
            }
            else{
                name2.add(date1+" Uhr : "+name+" ->  erreichbar ");
            }
        }
        startString = name+" wird überwacht. Taste drücken um abzubrechen...\n";
        String result = String.join("\n", name2);
        return startString+result;
    }
}

有人可以帮助我吗?因为我是PF和Ajax的新手.谢谢!

Can anybody help me? cause I'm new to PF and Ajax. Thanks!

推荐答案

我看到没有人在回答您的消息.因此,让我们尝试一下我的建议.

I see no-one is answering to your message. So let's try with my proposal.

如果您使用的是最新版本"的首字母缩写,那么我知道在投票中存在错误.

If you are using a "recent version" of primefaces, well I see there are mistakes in the call to the poll.

我正在使用PrimeFaces 5.1,如果您想通过javascript与民意测验进行交互,则解决方案是:

I am using PrimeFaces 5.1 and if you want to interact via javascript with your poll the solution is:

PF('poll').start();

PF('poll').stop();

我还建议避免在窗体上侦听键事件,而是使用jQuery在窗体或页面上注册事件侦听器,然后停止在使用的回调函数中进行轮询(给定此处).

I would like also to sugget to avoid in listening the key event on the form but use jQuery to register an event-listener on the form or on the page and than stop the polling in the call-back function thay uses (give a look here).

如果在更新结果时发现问题,请尝试更新其他结构

If you find a problem in updating your result, please try to update a different structure

<pre>
  <p:outputPanel id="output">
    <h:outputText value="#{helloBean.playground}" />
  </p:outputPanel>
</pre>
<p:poll interval="4" listener="#{helloBean.getPlayground}" 
        update="output" widgetVar="poll" autoStart="false" />

希望这会有所帮助! 祝你好运

Hope this will help! Good luck

Davide

这篇关于如何从javascript正确启动/停止PrimeFaces Ajax投票的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆