Duplicate Form Submission in Liferay 

Liferay form submissions are handled in  processAction method. In this post, we will see “How to prevents duplicate form submission in Liferay?”

High Level Steps:

  • add  “<action-url-redirect>true</action-url-redirect>” in liferay-portlet.xml on above”instanceable” attribute. Default action-url-redirect value is false, set to “true” to enable redirection
  • On Leaving action phase, portlet enters into render phase by copying all action parameters also. When user clicks on reload button after form browser causes duplication form submission and explained process here:
    • Browser  sends action request
    • Action URL processed at Controller
    • Sends success code to browser
  • doView() method will be invoked with the same URL which is having actionURL parameters. In this case, request again processed second time.

Solution to prevent duplication form submission in Liferay for action request:

Approach 1:

  • Get the current URL
    • String redirectUrl = PortalUtil.getCurrentURL(renderRequest);
  • set the redirectUrl for action Request
    • <portlet:actionURL copyCurrentRenderParameters="false" name="addData" var="addDataUrl">
       <portlet:param name="redirectUrl" value="<%=PortalUtil.getCurrentURL(renderRequest) %>"/>
       </portlet:actionURL>
  • Invoke actionResponse.sendRedirect(redirectUrl) to redirect to home page.
  • view.jsp
<portlet:actionURL copyCurrentRenderParameters="false" name="processAddItem" var="addItem">
 <portlet:param name="redirectUrl" value="<%=PortalUtil.getCurrentURL(renderRequest) %>"/>
 </portlet:actionURL>
<form method="post" action="<%=processAddItem.toString() %>">
 <aui:input type="text" /> 
 <aui:input type="button"/> 
 </form>

Approach 2:

Create Portlet URL for renderPhase to redirect:

 Create Portlet URL for render phase to redirectURL

String portletName= themeDisplay.getPortletDisplay().getPortletName();
 PortletURL portletURL = PortletURLFactoryUtil.create(PortalUtil.getHttpServletRequest(actionRequest),portletName,themeDisplay.getLayout().getPlid(),
 PortletRequest.RENDER_PHASE);
 portletURL.setParameter("id" String.valueOf(id));
 portletURL.setParameter("cmd","homeView");
 actionRequest.setAttribute(WebKeys.REDIRECT, portletURL.toString());
 // Here you can redirect to RedirectURL or Portlet URL 
 try {
 actionResponse.sendRedirect(portletURL.toString());
 } catch (IOException e) {

e.printStackTrace();
 }

 

 

Hope this helps….

Leave a Comment

Your email address will not be published. Required fields are marked *