我有一个ManagedBean来处理Item的列表,一个预定的线程每3秒添加一个新的Item并将该项推送到客户端,之后它通知页面必须更新ui:repeat组件以显示新的项目
的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://xmlns.jcp.org/jsf/html"
xmlns:p="http://primefaces.org/ui" xmlns:ui="http://java.sun.com/jsf/facelets"
xml:lang="en" lang="en">
<h:head>
<title>GG Well Trade</title>
</h:head>
<h:body>
<p:socket channel="/notify">
<p:ajax event="message" update="content"/>
</p:socket>
<p:commandButton value="Start" actionListener="#{bean.start()}"/>
<h:form id="content">
<ui:repeat value="#{bean.items}" var="item">
<p:outputLabel for="foo" value="#{item.label}" />
<p:inputText id="foo" value="#{item.value}" />
<p:commandButton value="Remove" actionListener="#{bean.remove(item)}" update="@form" />
<br/>
</ui:repeat>
</h:form>
</h:body>
</html>
Bean.class
@ManagedBean
@RequestScoped
@PushEndpoint("/notify")
public class Bean implements Serializable{
private List<Item> items;
@PostConstruct
public void init() {
items = new ArrayList<Item>();
}
public void start() {
ScheduledExecutorService timer = Executors.newSingleThreadScheduledExecutor();
timer.scheduleAtFixedRate(()->add(),0,3,TimeUnit.SECONDS);
}
public void add() {
Item item = new Item();
item.setLabel("label " + items.size());
items.add(item);
System.out.println(Thread.currentThread().getName()+ ": label" + items.size() + " added.");
EventBus eventBus = EventBusFactory.getDefault().eventBus();
eventBus.publish("/notify", item);
}
@OnMessage(encoders = {JSONEncoder.class})
public Item onMessage(Item item){
return item;
}
public void remove(Item item) {
items.remove(item);
}
public List<Item> getItems() {
return items;
}
}
在开发者控制台(Chrome)中收到请求,但不会更新表单。怎么了?
编辑:每个响应在开发控制台中都是这样的:
<?xml version='1.0' encoding='UTF-8'?>
<partial-response id="j_id1"><changes><update id="j_id1:javax.faces.ViewState:0"><![CDATA[-5528246304040933585:3098257791475168902]]></update></changes></partial-response>