Reputation: 6338
My JSF 2.0 page is contains some data, which is dynamic hence needs automatic refresh in some predefined time interval(say every 10 seconds). I am using PrimeFaces 3.5 as a powerful component suite. Below is the managed bean-
@ManagedBean
@ViewScoped
public Monitor implements Serializable {
//max,min,avg,stdDev,reason are caluclated based on some dynamic data
private int max;
private int min;
private double avg;
private double stdDev;
private String reason;
public int getMax() {
//Here before returning I am calling some methods to get the max from dynamic data
return max;
}
public int getMin() {
//Here before returning I am calling some methods to get the min from dynamic data
return min;
}
public double getAvg() {
//Here before returning I am calling some methods to get the avg from dynamic data
return avg;
}
public String getReason() {
//Here before returning I am calling some methods to get the reason from dynamic data
return reason;
}
public double getStdDev() {
//Here before returning I am calling some methods to get the stdDev from dynamic data
return stdDev;
}
public void setMax(int max) {
this.max = max;
}
public void setMin(int min) {
this.min = min;
}
public void setAvg(double avg) {
this.avg = avg;
}
public void setStdDev(double stdDev) {
this.stdDev = stdDev;
}
public void setReason(String reason) {
this.reason = reason;
}
}
I haven't finalize the layout till now however i would be like this-
<!DOCTYPE html>
<html lang="en" xmlns="http://www.w3.org/1999/xhtml"
xmlns:h="http://java.sun.com/jsf/html"
xmlns:p="http://primefaces.org/ui">
<h:head>
<title>Insert title here</title>
</h:head>
<h:body>
<p:panel>
<h:outputLabel value="#{monitor.min}" />
<h:outputLabel value="#{monitor.max}" />
<h:outputLabel value="#{monitor.avg}" />
<h:outputLabel value="#{monitor.stdDev}" />
<h:outputLabel value="#{monitor.reason}" />
</p:panel>
</h:body>
</html>
How can I update the page regularly in JSF 2.0?
Upvotes: 4
Views: 4212
Reputation: 4189
You can use Poll to auto refresh your page, Poll can start stop via javascript: for example, poll will stop when count(bean variable) >=10: You can use stop like @Ravi post, my example demo you can start stop(via: pol.stop() and pol.start()).
Facelets:
<h:form id="form">
<p:panel id="pntest">
// content here
</p:panel>
<h:outputText id="txt_count" value="#{tabview.count}" />
<p:poll interval="1" listener="#{tabview.increment}" update="txt_count" widgetVar="pol" oncomplete="test(xhr, status, args);"/>
<script type="text/javascript">
//<![CDATA[
function test(xhr, status, args){
if(args.sotest >= 10){
pol.stop();
}else{
location.reload(); // refresh page
}
}
//]]>
</script>
</h:form>
Bean:
private int count = 0;
public void increment() {
count++;
RequestContext reqCtx = RequestContext.getCurrentInstance();
reqCtx.addCallbackParam("sotest", count);
//1. update by using PrimeFaces specific API, use [RequestContext#update()][3].
RequestContext.getCurrentInstance().update("form:pntest");
//2. update by using standard JSF API, add the client ID to [PartialViewContext#getRenderIds()][4].
FacesContext.getCurrentInstance().getPartialViewContext().getRenderIds().add("form:pntest");
}
public int getCount() {
return this.count;
}
public void setCount(int count) {
this.count = count;
}
Upvotes: 2