Inter portlet communication using events:


In view.jsp of ipcexample1 portlet:
<%@ taglib uri="http://java.sun.com/portlet_2_0" prefix="portlet" %>
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>

<portlet:defineObjects />
<script type="text/javascript">
    function doSearch() {
        Liferay.fire('searchKeywords', document.getElementById("<portlet:namespace/>search").value);   
    }
</script>
<form>
    <input type="text" name="search" id="<portlet:namespace/>search" />
    <button name="Search" value="Search" onclick="doSearch()" type="button">Search</button>
</form>

Ipcexample1 portlet:
public class IPCPortlet extends MVCPortlet {
      public void serveResource(RenderRequest request,RenderResponse response)
      {
      PortletSession ps = request.getPortletSession();
      String tabNames = (String)ps.getAttribute("like",ps.APPLICATION_SCOPE);
      System.out.println(tabNames);
      }
}

In view.jsp of ipcexample2 portlet:
<%@ taglib uri="http://java.sun.com/portlet_2_0" prefix="portlet" %>

<portlet:defineObjects />
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ taglib uri="http://liferay.com/tld/aui" prefix="aui" %>

<portlet:defineObjects />
<portlet:resourceURL var="rurl" />

<script type="text/javascript">
    Liferay.on('searchKeywords', function(event, p_data){
        var A = AUI();
        A.use('aui-io-request', function(aui) {
            A.io.request("<%= rurl %>", {
                method : 'POST',
                data: {search: p_data},
                dataType : 'html',
                on : {
                    success : function() {
                        AUI().one("#<portlet:namespace/>placeholder").html(this.get('responseData'));
                    }
                }
            });
        });
    });
</script>
Search Results:<br />
<div id="<portlet:namespace/>placeholder">
</div>
In html.jsp of ipcexample2 portlet:

<font color="red"><%= request.getAttribute("result") %></font>

In ipcexample2 portlet
public class IPC2Example2 extends MVCPortlet {

      public void serveResource(ResourceRequest resourceRequest, ResourceResponse resourceResponse) throws PortletException, IOException {
        //do your search here and put results in 'result'
resourceRequest.setAttribute("result", "results for: " + resourceRequest.getParameter("search"));
getPortletContext().getRequestDispatcher("/html/ipc2/html.jsp").include(resourceRequest, resourceResponse);
Object o;//="sudheer";
PortletSession session=resourceRequest.getPortletSession();
session.setAttribute("like",143, PortletSession.APPLICATION_SCOPE);
}

}


Search This Blog

All the rights are reserved to this blog is belongs to me only.. Powered by Blogger.