当我们创建 JSF 页面时,客户端请求允许使用 Java 代码和 HTML 的组合动态生成 HTML。 我们能否使用 JSF 框架在 HTML 页面中引入钩子(Hook),允许服务器根据稍后在服务器上发生的异步事件(通常通过不同的线程)更新 HTML 页面?
最佳答案
JSF 2.3+
您可以使用 @Push
and <f:websocket>
为了这。下面是一个带有应用程序范围套接字的启动示例,该套接字在后端通过 Event#fire()
触发的事件时更新数据表。哪个托管 bean @Observes
.
<h:dataTable id="notifications" value="#{bean.notifications}" var="notification">
<h:column>#{notification.message}</h:column>
</h:dataTable>
<h:form>
<f:websocket channel="push">
<f:ajax event="updateNotifications" render=":notifications" />
</f:websocket>
</h:form>
@Named @ApplicationScoped
public class Bean {
private List<Notification> notifications;
@Inject
private NotificationService service;
@Inject @Push
private PushContext push;
@PostConstruct
public void load() {
notifications = service.list();
}
public void onNewNotification(@Observes Notification newNotification) {
notifications.add(0, newNotification);
push.send("updateNotifications");
}
public List<Notification> getNotifications() {
return notifications;
}
}
@Stateless
public class NotificationService {
@Inject
private EntityManager entityManager;
@Inject
private BeanManager beanManager;
public void create(String message) {
Notification newNotification = new Notification();
newNotification.setMessage(message);
entityManager.persist(newNotification);
beanManager.getEvent().fire(newNotification);
}
public List<Notification> list() {
return entityManager
.createNamedQuery("Notification.list", Notification.class)
.getResultList();
}
}
JSF 2.2-
如果您尚未使用 JSF 2.3,则需要前往第 3 方 JSF 库。
- OmniFaces有
<o:socket>
(JSR356 WebSocket + CDI) - PrimeFaces有
<p:socket>
(气氛) - ICEfaces有“ICEpush”(长轮询)
值得注意的是 <o:socket>
是 JSF 2.3 <f:websocket>
的基础。因此,如果您发现了很多相似之处,那么这是正确的。
PrimeFaces 使用 Atmosphere在幕后(如果没有 Maven,设置起来很麻烦)。 Atmosphere 支持 Websocket,并可回退到 SSE 和长轮询。 ICEfaces 基于古 long polling技术。所有这些都没有实现 native JSR356 WebSocket API,该 API 后来才在 Java EE 7 中引入。
OmniFaces 使用原生 JSR356 WebSocket API (所有 Java EE 7 服务器和 Tomcat 7.0.27+ 均受支持)。因此,它的设置和使用也是最简单的(一个 JAR、一个上下文参数、一个标签和一个注释)。它只需要 CDI(install on Tomcat 并不难),但它甚至使您能够从非 JSF 工件进行推送(例如 @WebServlet
)。出于安全性和 JSF View 状态保持原因,它仅支持单向推送(服务器到客户端),而不支持相反的方式。为此,您可以继续以通常的方式使用 JSF ajax。 JSF 2.3 <f:websocket>
很大程度上基于 OmniFaces <o:socket>
,因此您会发现它们的 API 有很多相似之处 ( JSF - OmniFaces )。
或者,您也可以使用轮询而不是推送。几乎每个支持 ajax 的 JSF 组件库都有一个 <xxx:poll>
组件,例如带有 <p:poll>
的 PrimeFaces 。这允许您每 X 秒向服务器发送一个 ajax 请求,并在必要时更新内容。只是效率比推送低。
另请参阅:
关于jsf - 服务器如何将异步更改推送到 JSF 创建的 HTML 页面?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5391027/