actionPoller in visualforce

Why are we using actionpoller in visualforce ?

Before Technical Explanation, let me explain a real time example on using actionpoller in visualforce, Let’s take Crickbuzz.com site where scorecard will be automatically refreshed during certain interval, like in the same way we can use actionpoller in visualforce to make ajax request to server without reloading the page according to the time interval specified by us.

Point to remember:-

  • You need to use actionpoller in visualforce(<apex:actionPoller>) inside <apex:actionRegion>, if not whole form(<apex:form>) being submitted to server and if there is any mandatory fields is there then validation rule will get fired.
  • In order to avoid that, action region provides the functionality for partial pageblock or field refresh where we can avoid whole form(<apex:form>) getting submitted to server.
  • As it will be periodically getting refreshed, user session remains to be alive. it won’t time out due to user being idle.
  • Action method you wire with actionpoller in visualforce should be light weight and avoid performing DML operation.

In the below example, i am just incrementing the counter value during every 5 seconds.

Action method ‘CountIncMethod’ wired with action poller which will make ajax request for every 5 seconds so eventually th counter variable keeps on incrementing infinitely.

Action status (<apex:actionStatus>) also i used along with action poller, where action status has child tag <apex:facet> it has start and stop attribute.You can define what you needs to display either a spinner or some text using ‘start’ attribute during server request, once the response received from server you can use ‘stop’ attribute to display some text.

 

Apex Class(actionpoller in visualforce) :-

public class ActionPollerController{
     Integer counter = 0;
             
     public PageReference CountIncMethod() {
         counter++;
         return null;
     }
             
     public Integer getCounter() {
         return counter;
     }
 }

Visualforce Page (actionpoller in visualforce) :-

<apex:page controller="ActionPollerController">
 <apex:form >
 
 <apex:outputText value="Counter getting incrementing automatically through ActionPoller : {!counter}" id="counterId"/>
 <apex:actionPoller action="{!CountIncMethod}" reRender="counterId" interval="5" status="statusId"/>
 
 &nbsp;
 
 <apex:actionStatus id="statusId">
 <apex:facet name="start">Requesting server please wait..<img src="/img/loading.gif"/></apex:facet>
 </apex:actionStatus>
 
</apex:form>
</apex:page>

Please click the below links for more ajax vf components.

ActionFunction Article * Actionfunction and ActionSupport comparison Article * Immediate Attriute Article * Rendered Attribute Example

Be the first to comment

Leave a Reply

Your email address will not be published.


*