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:
我想要一个小webapp来测试到服务器的连接并返回状态消息。单击“开始”按钮后,我希望每4秒进行轮询,并返回状态文本。但它只会返回,如果我点击按钮,不是自动的。这是我的代码:
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>
And my bean:
我的豆:
@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;
}
}
Can anybody help me? cause I'm new to PF and Ajax. Thanks!
有人能帮助我吗?因为我是PF和Ajax新手。谢谢!
1 个解决方案
#1
3
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.
如果你使用的是“最近版本”的原始面孔,那么我发现在对投票的呼吁中存在错误。
I am using PrimeFaces 5.1 and if you want to interact via javascript with your poll the solution is:
我使用的是PrimeFaces 5.1,如果你想通过javascript与你的投票进行交互,解决方案是:
PF('poll').start();
and
和
PF('poll').stop();
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).
我还希望sugget避免在监听窗体上的关键事件时使用jQuery在窗体或页面上注册事件侦听器,而不要在thay使用的回调函数中停止轮询(请在这里查看)。
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
大卫。
#1
3
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.
如果你使用的是“最近版本”的原始面孔,那么我发现在对投票的呼吁中存在错误。
I am using PrimeFaces 5.1 and if you want to interact via javascript with your poll the solution is:
我使用的是PrimeFaces 5.1,如果你想通过javascript与你的投票进行交互,解决方案是:
PF('poll').start();
and
和
PF('poll').stop();
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).
我还希望sugget避免在监听窗体上的关键事件时使用jQuery在窗体或页面上注册事件侦听器,而不要在thay使用的回调函数中停止轮询(请在这里查看)。
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
大卫。