我通过这个简单的代码解决了问题。(我为你分享了这段代码)
public class Information {
private String name ;
private String family ;
// constructor
// Getter & Setter
// override equal and hashCode
}
这是一个简单的服务。(我在这个类上模拟了数据库)
@Stateless
public class InformationService {
private static final List<Information> db = new ArrayList<>();
@Inject
@Push(channel = "infoChannel")
PushContext push;
@PostConstruct
public void init() {
Information userA = new Information("John", "Vankate");
Information userB = new Information("Julius", "Sampao");
db.add(userA);
db.add(userB);
}
public void remove(Information info) {
db.remove(info);
push.send("deleteInfo");
}
public List<Information> findAll() {
return db;
}
}
和简单的 JaxRs 资源:
@Path("/info")
@RequestScoped
public class InformationResources {
@EJB
private InformationService informationService;
@Path("/delete")
@POST
@Consumes("application/json")
public String send(Information information) {
informationService.remove(information);
return "Receive : " + information;
}
}
现在启动 JSF:
@Named
@ViewScoped
public class InformationBean implements Serializable {
private Information info ;
private List<Information> informationList ;
@EJB
private InformationService informationService ;
@PostConstruct
public void init() {
informationList = informationService.findAll();
info = new Information() ;
}
public void deleteInformation() {
informationService.remove(info);
}
public Information getInfo() {
return info;
}
public void setInfo(Information info) {
this.info = info;
}
public List<Information> getInformationList() {
return informationList;
}
public void setInformationList(List<Information> informationList) {
this.informationList = informationList;
}
}
和 xhtml :
<h:body>
<p:dataTable value="#{informationBean.informationList}" var="info" id="infoTable">
<p:column rowHeader="name">
<h:outputText value="#{info.name}"/>
</p:column>
<p:column rowHeader="family">
<h:outputText value="#{info.family}"/>
</p:column>
<p:column rowHeader="action">
<h:form>
<p:commandButton value="Delete" action="#{informationBean.deleteInformation}">
<f:setPropertyActionListener value="#{info}" target="#{informationBean.info}"/>
</p:commandButton>
</h:form>
</p:column>
</p:dataTable>
<hr/>
<f:websocket channel="infoChannel">
<p:ajax event="deleteInfo" update="infoTable"/>
</f:websocket>
</h:body>
我已经想过,PushContext 必须在 JSF bean 上实现,现在我明白可以在服务或业务逻辑层实现。
现在您可以从 JaxRs (Rest API) 中删除信息并p:dataTable
从不刷新页面中删除记录。
注意:此示例使用@ViewScoped