dmh
dmh

Reputation: 430

Auto Refresh Portlet Liferay 6.0(Periodically refresh)

I want To create Portlet For Monitoring Something, so it need like automatically refresh portlet page every interval of time, how i can achieve this? I've been trying with normal method like using Javascript but its didn't work... Thanks, please give me example :(

any help would be really appreciate i'm trying using normal code for jsp but it's cant run

<%@ page import="java.io.*,java.util.*" %>
<html>
<head>
<title>Auto Refresh Header Example</title>
</head>
<body>
<center>
<h2>Auto Refresh Header Example</h2>
<%
   // Set refresh, autoload time as 5 seconds
   response.setIntHeader("Refresh", 5);
   // Get current time
   Calendar calendar = new GregorianCalendar();
   String am_pm;
   int hour = calendar.get(Calendar.HOUR);
   int minute = calendar.get(Calendar.MINUTE);
   int second = calendar.get(Calendar.SECOND);
   if(calendar.get(Calendar.AM_PM) == 0)
      am_pm = "AM";
   else
      am_pm = "PM";
   String CT = hour+":"+ minute +":"+ second +" "+ am_pm;
   out.println("Crrent Time: " + CT + "\n");
%>
</center>
</body>
</html>

Regards

Danial

Upvotes: 1

Views: 3100

Answers (3)

dmh
dmh

Reputation: 430

I'm Managed to solve this problem using this code

<%@page import="com.liferay.portal.kernel.portlet.LiferayWindowState"%>
<%@page import="java.util.Date"%>
<%@ taglib uri="http://java.sun.com/portlet_2_0" prefix="portlet" %>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"></script>

 <script type="text/JavaScript">
 <!--
 function timedRefresh(timeoutPeriod) {

 $.post('<portlet:renderURL windowState="<%= LiferayWindowState.EXCLUSIVE.toString() %>"></portlet:renderURL>', function(data){
     $("#myportlet").html(data);
 })
}
timedRefresh(5000);
//   -->

</script>
<div id="myportlet"><%= new Date() %></div>

Thanks to @boky who give me the main idea how to solve this problem :)

Regards

Danial

Upvotes: 1

boky
boky

Reputation: 835

Setting the Refresh header will in -- best case scenario -- refresh the whole page (that's what it's meant to do) and not just your portlet.

How you set up this refresh depends on your underlying technology for writing the portlet. Basically you want to do an AJAX request to your page to fetch the new data and redisplay it, as @harishkrsingla suggested.

If your code is pure JSP, you would set up two pages: - one for displaying the portlet - the other that's rendering the content

Your portlet page would then look something like this (really writting this off the top of my head, check documentation online):

  <div id="portlet">
     <jsp:include file="data.jsp" />
  </div>
  <script type="text/javascript">
     // assuming jquery
     var load;
     load = function() {
         $('#portlet').load('<portlet:namespace />/data.jsp', function() {
            window.setTimeout(load, 1000);
         });
     }
     load();
  </script>

Also check out the working demo on JSFiddle: http://jsfiddle.net/z9az9/1/

Of course, this is just the basic idea. You should really include some error handling etc.

Upvotes: 0

harishkrsingla
harishkrsingla

Reputation: 1

If you want update your portlet at regular interval, you can make use of serveResource method.

Make an ajax call to serveResource method and you can set setTimeout on this ajax call. Following is the sample code snippet -

function <portlet:namespace />get_updated_data() {
        var f = jQuery.ajax({
        type: "POST",
        url: '<<resourceUrl>>',
        data:  {"name" : "val"},
        dataType: 'json',
        async: false,
     }).success(function(data){
        // success code here             
    }).complete(function(){
        setTimeout(function(){<portlet:namespace />get_updated_data();}, 5000);
    });

}

Upvotes: 0

Related Questions