我正在尝试在我的项目中实现主推计数器。我使用的是PrimeFaces3.5,Jboss7.0和Eclipse Indigo版本。
我添加了与推送相关的罐子:
我的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:f="http://java.sun.com/jsf/core" xmlns:h="http://java.sun.com/jsf/html"
xmlns:ui="http://java.sun.com/jsf/facelets" xmlns:p="http://primefaces.org/ui">
<h:head></h:head>
<h:body>
<h:form id="form">
<h:outputText id="out" value="#{pushBean.count}" />
<p:commandButton value="Click" actionListener="#{pushBean.increment}" />
</h:form>
<p:socket onMessage="handleMessage" channel="/counter" />
<script type="text/javascript">
function handleMessage(data) {
$('.display').html(data);
}
</script>
</h:body>
</html>
我的托管bean:
@ManagedBean(name = "pushBean")
@ApplicationScoped
public class PushBean {
public PushBean() {
}
private int count;
public int getCount() {
return this.count;
}
public void setCount(final int count) {
this.count = count;
}
public synchronized void increment() {
this.count++;
PushContext pushContext = PushContextFactory.getDefault().getPushContext();
pushContext.push("/counter", String.valueOf(this.count));
}
}
当我单击UI中的按钮时,计数会在服务器上递增,但它不会自动反映在UI中,因为它未更新。但是当我刷新页面时,计数会按预期递增。
我得到的例外是:
13:00:02,298 ERROR [stderr] (http--0.0.0.0-8080-5) [http--0.0.0.0-8080-5] ERROR org.atmosphere.cpr.AtmosphereFramework - AtmosphereFramework exception
13:00:02,298 ERROR [stderr] (http--0.0.0.0-8080-5) java.lang.IllegalStateException: The servlet or filters that are being used by this request do not support async operation
答案 0 :(得分:2)
当您的Web应用程序在符合Servlet 3的容器中运行时,会发生此异常。正确的web.xml必须将async支持的元素设置为true,例如:
<servlet>
<servlet-name>Push Servlet</servlet-name>
<servlet-class>org.primefaces.push.PushServlet</servlet-class>
<async-supported>true</async-supported>
</servlet>
答案 1 :(得分:0)
我对推送框架不太确定,但我认为问题出在你的javascript上。 你正在努力刷新&#34; .display&#34;控制,但没有像.display。
尝试&#39; .out&#39;而不是.display