Search code examples
javajavascriptliferayportletliferay-6

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


Solution

  • 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