Avoiding Multiple Submits(Refresh page) in Liferay


We often see after submitting the form and refresh the page you will see the same result will be adding in the DB.

So how to get rid of this?

Here we have 2 options to eliminate this behaviour.

1. By Declaring the tag in the liferay-portlet.xml after <icon> tag

          <action-url-redirect> true </action-url-redirect>

2.After doing our logic we can redirect to specified URL by using actionResponse.

   actionResponse.sendRedirect(PortalUtil.getLayoutURL(themeDisplay.getLayout(),themeDisplay));

  Here we are  redirecting to the friendly url of that page.

The above will be redirect to the below URL as
                   //group/test/test1----- (group/<your-community-name>/pageName).

Comments

  1. Hi
    I followed your example true for avoid duplicate submission in forms and redirected to another page after the submission by response.setRenderParameter("jspPage","page"); and forward the value by request.setAttribute("","");

    But I can't get the value in the page request.getAttribute() method. It displaying null. But its working if I removed true in liferay-portlet.xml.
    I need both should be work. Can you please help me on this?

    ReplyDelete
  2. Hi Arasan,


    See this thread " http://www.liferay.com/community/forums/-/message_boards/message/13943569 ".

    They are using the following snipets to redirect

    String portletName = (String)actionRequest.getAttribute(WebKeys.PORTLET_ID);

    ThemeDisplay themeDisplay = (ThemeDisplay) actionRequest.getAttribute(WebKeys.THEME_DISPLAY);

    PortletURL redirectURL = PortletURLFactoryUtil.create(PortalUtil.getHttpServletRequest(actionRequest), portletName,
    themeDisplay.getLayout().getPlid(), PortletRequest.RENDER_PHASE);

    redirectURL.setParameter("jspPage", "your jsp path");

    actionResponse.sendRedirect(redirectURL.toString());

    ReplyDelete
    Replies
    1. Let me know if you required something else

      Delete
    2. Thanks Mr. Venka for your response,
      I asked the solution for side effect of disallowing to use share the value between two pages by request.getAttribute("")
      Eventhough your code works fine, I can't share the value by request.getAttribute("") method. By removing true only I can share the value by request.getAttribute("") method. It's my issue.

      Delete
    3. By Accessing via portlet session only I can able to share

      Delete
    4. Hi tkarasan,

      I fill explain you step by step how to achive that

      1. In liferay-portlet.xml : true

      2. In class

      actionResponse.setRenderParameter("name", "venka");

      3. In JSP

      if (request.getParameter("name") == null) {
      out.println("value is null.");
      } else {
      out.println("Hello your name is "+request. getParameter("name")+"!");
      }

      I have tested this . Don't use session it may have unnecessarily probs

      Let me know if anything else required.

      Delete
    5. Also Thanks for pointing out. I will update blog.

      Delete
    6. This comment has been removed by the author.

      Delete
    7. Thanks Mr.Venka

      Your code works fine. Thank you for your valuable reply.

      Delete
    8. Thanks, Good to hear from you..

      Delete
    9. Hi,
      I am using the doView and render method in the same portlet, in which I have added the above tag. In this method will not support for setRenderParameter(String,String); Because it will support for Action methods only. Then how I can pass the value from doView or render method to jsp page.

      Delete
  3. Hello I have tried this its working..but it is not giving me the success message...

    ReplyDelete

Post a Comment

Popular posts from this blog

Theme display in javascript

How to know which liferay version we are using

Opening portlet in popup