我正在尝试使用primepush添加行后更新数据表。意思是,我希望user1更新数据表,user2应该看到添加的行而不刷新页面。
My Primefaces版本为5.2,Atmosphere运行时版本为2.3.3
这是我尝试基于Primefaces Showcase和用户指南实现的相关代码:
的web.xml:
<web-app xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
version="3.0">
<display-name>PrimeFaces Web Application</display-name>
<context-param>
<param-name>javax.faces.PROJECT_STAGE</param-name>
<param-value>Development</param-value>
</context-param>
<context-param>
<param-name>javax.faces.DEFAULT_SUFFIX</param-name>
<param-value>.xhtml</param-value>
</context-param>
<welcome-file-list>
<welcome-file>faces/index.xhtml</welcome-file>
</welcome-file-list>
<filter>
<filter-name>AccessFilter</filter-name>
<filter-class>view.AccessFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>AccessFilter</filter-name>
<url-pattern>/secured/*</url-pattern>
</filter-mapping>
<servlet>
<servlet-name>Faces Servlet</servlet-name>
<servlet-class>javax.faces.webapp.FacesServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet>
<servlet-name>Push Servlet</servlet-name>
<servlet-class>org.primefaces.push.PushServlet</servlet-class>
<async-supported>true</async-supported>
</servlet>
<servlet-mapping>
<servlet-name>Faces Servlet</servlet-name>
<url-pattern>/faces/*</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>Faces Servlet</servlet-name>
<url-pattern>*.jsf</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>Faces Servlet</servlet-name>
<url-pattern>*.faces</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>Faces Servlet</servlet-name>
<url-pattern>*.xhtml</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>Push Servlet</servlet-name>
<url-pattern>/primepush/*</url-pattern>
</servlet-mapping>
</web-app>
支持Bean:
@ManagedBean
@ViewScoped
@PushEndpoint("/main")
public class MainBean implements Serializable{
...
public void addContact(ActionEvent event){
... // database operations
Contact con = new Contact();
contacts.add(con); // this is an ArrayList
EventBus eventBus = EventBusFactory.getDefault().eventBus();
eventBus.publish("/main", contacts);
}
@OnMessage(encoders = {JSONEncoder.class})
public ArrayList<Contact> getContacts(){
return contacts;
}
}
main.xhtml:
...
<p:commandButton value="Add Contact" update=":form:datatable"
actionListener="#{mainBean.addContact}" ajax="true"/>
<h:form id="form">
<p:dataTable id="datatable" var="contact" value="#{mainBean.contacts}"
selectionMode="single" rowKey="#{contact.firstName}"
selection="#{mainBean.selectedContact}">
<p:column headerText="First Name">
<h:outputText value="#{contact.firstName}"/>
</p:column>
<p:column headerText="Last Name">
<h:outputText value="#{contact.lastName}"/>
</p:column>
</p:dataTable>
</h:form>
<p:socket channel="/main">
<p:ajax event="message" update=":form:datatable"/>
</p:socket>
当我尝试向表中添加一行时,不会发生错误,但有些行会丢失。当我刷新时,表重新加载它应该的方式。添加的行也不会在不同用户之间传递。在user1向表中添加值后,User2仍需要刷新才能看到更改。
我也尝试了this link中的建议但没有成功。我对Primepush很新,我在网上找到的例子(除了用户指南)都是针对真正旧版本的Primefaces,推送功能似乎在Primefaces 5之后发生了很大变化。感谢任何帮助。
编辑2:如果推送正在工作,我已经记录了,似乎确实如此。当我导航到启用了推送的页面时,我可以在控制台上观察它是否被激活。但是,当我尝试更新表时,填充表的列表的get方法在推送后连续三次被调用...