Cookies help us deliver our services.

By using our services, you agree to our use of cookies. Learn more

I understand

Almost a year ago I wrote an article (How to close a Dialog IFrame in Liferay 6.2) about how to close a Dialog IFrame in Liferay 6.2.

The content of that article is still valid, but now we'll see an alternative more elegant method to do the same thing.

Let's start from the beginning and define the button which will open the dialog in the /html/dialog/view.jsp page.

<liferay-portlet:renderURL var="dialogURL" windowState="<%=LiferayWindowState.POP_UP.toString() %>">
    <liferay-portlet:param name="mvcPath" value="/html/dialog/dialog.jsp" />
</liferay-portlet:renderURL>
<aui:button href="/<%=dialogURL %>" primary="true" useDialog="true" value="open-dialog" />

The code above does not require any explanation, it just opens a render url in a dialog, using the useDialog new attribute introduced in Liferay 6.2.

Then we define an HTML form in the dialog page, namely /html/dialog/dialog.jsp.

<liferay-portlet:renderURL var="redirectURL">
    <liferay-portlet:param name="mvcPath" value="/html/dialog/dialog.jsp" />
</liferay-portlet:renderURL>

<liferay-portlet:actionURL name="save" var="saveURL" /> <aui:form action="<%=saveURL %>" method="post" name="fm"> <aui:input name="redirect" type="hidden" value="<%=redirectURL %>" /> <!-- Some fields --> <aui:button-row> <aui:button type="submit" value="save" /> </aui:button-row> </aui:form>

I stress the importance of the definition of the redirect parameter that indicates to Liferay the page to which redirect (not forward) at the end of the processAction; in this case we are going to reload the same page of the form itself. Do not forget that all the processing is still confined within the dialog.

The focus now is knowing that the Liferay infrastructure, at the end of a successful processAction, put the requestProcessed attribute in session; we can just look for this attribute, once the page is reloaded in the dialog, to know that processing is complete and that we can close the dialog.

The code of the /html/dialog/dialog.jsp page must be slightly modified.

<c:choose>
<c:when test='<%=SessionMessages.contains(renderRequest, "requestProcessed") %>'>
<aui:script>
// If needed Liferay.Util.getOpener().<portlet:namespace />aMethod();
Liferay.Util.getOpener().<portlet:namespace />anotherMethod();
// Mandatory to close the dialog Liferay.Util.getWindow().hide(); </aui:script>
</c:when>
<c:otherwise>
<liferay-portlet:renderURL var="redirectURL"> <liferay-portlet:param name="mvcPath" value="/html/dialog/dialog.jsp" /> </liferay-portlet:renderURL>
<liferay-portlet:actionURL name="save" var="saveURL" /> <aui:form action="<%=saveURL %>" method="post" name="fm"> <aui:input name="redirect" type="hidden" value="<%=redirectURL %>" /> <!-- Some fields --> <aui:button-row> <aui:button type="submit" value="save" /> </aui:button-row> </aui:form>
</c:otherwise>
</c:choose>

As you can see the page processing follows a different flow according to the SessionMessages.contains (renderRequest, "requestProcessed") test.

In particular, you can invoke JavaScript functions defined, by Liferay.provide, in the caller JSP (/html/dialog/view.jsp page) through the Liferay.Util.getOpener() object; then we can finally invoke the Liferay.Util.getWindow().hide() function to close the dialog.

As you can see, the solution is very clean and elegant although required a bit of work.