Search code examples
javajspliferayportlet

event for portlets in different page doesnt reload view.jsp


I have two portlets and am trying to send a event from say portletA to portletB. Now the problem is that when i keep both the portles in same page, it works but when i keep them in different pages, the view.jsp page of PortletB doesnt refresh even though control comes to the the method ProcessAction of portletB(values i am priting gets printed so i guess control comes to that part).

Portlet A view.jsp

<portlet:actionURL var="changedb" name="processEvent"/>


<aui:form method="post" action="<%=changedb.toString() %>">
<aui:fieldset>
    <aui:select label="Select Bot" id="options" name="botname" 
required="true" showEmptyOption="true">
        <aui:option value="otion1" name="option1" 
>option1</aui:option>
        <aui:option value="option2" name="option2" 
>option2</aui:option>
    </aui:select>
<aui:button type="submit" value="Send"/>
</aui:fieldset>
</aui:form>

Java method :

@ProcessAction(name="processEvent")
public void process(ActionRequest request, ActionResponse response) {
    String bot = ParamUtil.getString(request, "botname","");
    String url = "somehttplink" + bot;
    System.out.print("control came inside changedbportlet");
    System.out.println(url);
    QName qName = new QName("botchange");
    response.setEvent(qName, url);

}

Portlet B(receiver)

<portlet:defineObjects />

<%
String url = (String) renderRequest.getParameter("url");
%> 
<main class="container">
<div class="row">
<div class="col-lg-12">
    <div id="initial-screen">
 <iframe class="iframe" src="<%= url %>"  width = "1000" height="800" 
></iframe>
</div>
</div>
</div>
</main> 

Java method :

@ProcessEvent(qname = "botchange")
public void myEvent(EventRequest request, EventResponse response)
        throws javax.portlet.PortletException,
    java.io.IOException {
    Event event = request.getEvent();
    String url = (String) event.getValue();
    System.out.println("control came to showpageportlet");
    System.out.print(url);
    response.setRenderParameter("url", url);
}

i am using Liferay 7 and osgi modules.I have also added below command in the portlet-ext.properties file as per the instructions in https://web.liferay.com/community/wiki/-/wiki/Main/portlet+to+portlet+communication.

portlet.event.distribution=layout-set

What am i doing wrong?


Solution

  • In the scenario where PortletA sends an information to PortletB that needs to change its state but it is not on the current page you need to have a way how to remember this fact.

    The event just transmits the information but the ProtletB is responsible for remembering it.

    You could use a session attribute where you store the value or even better a portlet preference that will be edited in the event handler method and read in the render (doView) method. Don't forget to have a default.

    you can store the value in the event with this.

    PortletPreferences preferences = request.getPreferences();
    preferences.setValue("url", url);
    preferences.store();
    

    And then access it wia this.

    request.getPreferences();
    String state = (String) preferences.getValue("url", "")