Saturday, 22 August 2015

Refresh page automatically using PollEvent to show current time.

Requirement: There is a page demo.jsf. In that page we will have an af:outputText. The value in that af:outputText will be the datetime in "yyyy.MM.dd.HH.mm.ss" format. Now when the page load it shows value in the af:outputText. Also every second the value should refresh automatically and display the current datetime in the af:outputText at that point of time.

Solution: For solution of the above requirement follow the below steps:

Step 1: Create an Oracle ADF Fusion Web Application.

Step 2: Create a demo.jsf page in the ViewController project of our application created in Step 1

Step 3: Drag and drop af:outputText from the component palette in the demo.jsf page created in Step 2.

Step 4: Drag and drop af:poll from the component palette in the demo.jsf page created in Step 2. From property inspector set interval="1000". Create a pollListener of the af:poll from the property inspector of the af:poll. The Bean Name is MyBean, the Class Name is MyBean, the scope of the Managed Bean is backingBeanScope, keep the Package as view, Extends is java.lang.Object. Click OK.
Give the pollListener name as getTime.

Step 5: Set partialTriggers="p1" from the property inspector of the af:outputText. This will refresh the af:outputText whenever the af:poll is called.

Step 6: Go to MyBean.java file. Add private String dateTime = null; Generate the accessors for dateTime String.

Step 7: Now go to getTime(PollEvent pollEvent) method in the MyBean.java file and write the below code:
        String timeStamp = new SimpleDateFormat("yyyy.MM.dd.HH.mm.ss").format(new Date( ));
        this.setDateTime(timeStamp);


Thus the complete MyBean.java code is shown below:

Step 8: Go to af:outputText in demo.jsf page. Set the value of the af:outputText as value="#{backingBeanScope.MyBean.dateTime}". Thus the complete demo.jsf page code is shown below:

Step 9: Save All and run the application. The running application is shown as below:

The demo.jsf page is loaded automatically every second and the value in the af:outputText is also getting refreshed.

Thanks & Regards,
Susanto Paul