Reputation: 50
My goal is to display a text on a web page, with JSF and/or PrimeFaces. The text is updated in real time by a process on the server side.
<?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>
My backing bean maps the text to p:outputLabel. I also added to the bean a method foo() that modifies the text. This method is called by another process running in the background (it holds a pointer to the 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 ?
}
}
What I cannot do so far, is to update/refresh the view automatically each time the text is updated. The best I could do is to let the user manually refresh the web page, or to use p:poll to refresh at regular intervals.
I have researched this, and I have found very similar threads; the answer is usually to use FacesContext.getCurrentInstance() (JSF) or RequestContext.getCurrentInstance() (PrimeFaces) to access the component and refresh it. I tried, but the context is not available in foo().
I guess it's a lifecycle issue. I have a very limited knowledge of JSF. Any help is appreciated.
Upvotes: 0
Views: 1558
Reputation: 12337
You can't access a faces context from a bean when it is updated from a non jsf backend call. Use PrimeFaces push for this.
Upvotes: 0