[英]Refreshing a JSF/PrimeFaces interface from the backing Bean
我的目標是使用JSF和/或PrimeFaces在網頁上顯示文本。 文本由服務器端的進程實時更新。
<?xml version="1.0" encoding="ISO-8859-1" ?>
<!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:p="http://primefaces.org/ui">
<h:body>
<f:view id="view">
<f:event type="preRenderView" listener="#{MyBean.init}"/>
<h:form>
<p:outputLabel id="output" value="#{MyBean.text}"/>
</h:form>
</f:view>
</h:body>
</html>
我的支持bean將文本映射到p:outputLabel。 我還向Bean添加了修改文本的方法foo()。 此方法由在后台運行的另一個進程調用(它持有指向Bean的指針)。
@ManagedBean(name="MyBean")
@SessionScoped
public class MyBean {
private String text = "text";
public void init() {
MyOtherClass.getPointer(this);
}
public String getText() {
return text;
}
public String foo(String s) {
text = text + s;
// ...and refresh ?
}
}
到目前為止,我無法做的是每次文本更新時自動更新/刷新視圖。 我能做的最好的事情就是讓用戶手動刷新網頁,或者使用p:poll定期刷新。
我對此進行了研究,發現非常相似的線程。 答案通常是使用FacesContext.getCurrentInstance()(JSF)或RequestContext.getCurrentInstance()(PrimeFaces)訪問組件並刷新它。 我試過了,但是上下文在foo()中不可用。
我想這是一個生命周期問題。 我對JSF的了解非常有限。 任何幫助表示贊賞。
從非jsf后端調用更新后,您將無法從bean訪問faces上下文。 為此使用PrimeFaces推送。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.