Forums

Home » Liferay Portal » English » 3. Development

Combination View Flat View Tree View
Threads [ Previous | Next ]
toggle
devi nimmagadda
Duplicate Submission of Form
June 6, 2011 3:43 AM
Answer

devi nimmagadda

Rank: Regular Member

Posts: 109

Join Date: December 9, 2010

Recent Posts

Hi,


can anybody know this....

How to prevent duplicate submission of a form ?

Because I am submitting a form with data. When I click on submit button the row is inserted into the database table.

When I refresh the page or go to back button of the same page, The row is inserted again.

Can anybody give suggestion to this.....

Multiple rows are inserting in my table.

Thanks in advance
Hon Hwang
RE: Duplicate Submission of Form
July 16, 2011 9:49 PM
Answer

Hon Hwang

Rank: New Member

Posts: 17

Join Date: July 3, 2011

Recent Posts

I had similar problem and found a solution. If you are using POST to submit your form, found that this is HTTP POST double submit problem.

Solution is to send a redirect after your action method.
Roshan Qureshi
RE: Duplicate Submission of Form
July 17, 2011 10:51 PM
Answer

Roshan Qureshi

Rank: Regular Member

Posts: 144

Join Date: August 24, 2010

Recent Posts

This problem is related to any jsp or servlets. Whenever you are submitting any form after that you come back to that page again.
To prevent duplication you should use redirect method instead of using forward method.
This problem is also called as "refresh problem".

Thanks,
Roshan Qureshi
Ketan Savaliya
RE: Duplicate Submission of Form
July 17, 2011 11:00 PM
Answer

Ketan Savaliya

Rank: Junior Member

Posts: 77

Join Date: March 3, 2011

Recent Posts

devi nimmagadda:
Hi,


can anybody know this....

How to prevent duplicate submission of a form ?



Hi, devi nimmagadda
Try this,
1) get PortletURL from actionrequest in action method

ThemeDisplay themeDisplay = (ThemeDisplay) actionRequest.getAttribute(WebKeys.THEME_DISPLAY);
String portletName = (String)actionRequest.getAttribute(WebKeys.PORTLET_ID);
PortletURL redirectURL = PortletURLFactoryUtil.create(PortalUtil.getHttpServletRequest(actionRequest), portletName,
themeDisplay.getLayout().getPlid(), PortletRequest.RENDER_PHASE);

2) if u want to set parameter then set to PortletURL

redirectURL.setParameter("view", "abc");

3) at last redirect by actionResponse

actionResponse.sendRedirect(redirectURL.toString());

Hope so it will help u. emoticon
Thanks,
Ketan
KK rajput
RE: Duplicate Submission of Form
July 18, 2011 5:16 AM
Answer

KK rajput

Rank: Expert

Posts: 266

Join Date: April 10, 2008

Recent Posts

devi nimmagadda:
Hi,


can anybody know this....

How to prevent duplicate submission of a form ?

Because I am submitting a form with data. When I click on submit button the row is inserted into the database table.

When I refresh the page or go to back button of the same page, The row is inserted again.

Can anybody give suggestion to this.....

Multiple rows are inserting in my table.

Thanks in advance

check below link, it might be useful. If you are having portal source then you can see test redirect mapping in struts-config.
http://kamalkantrajput.blogspot.com/2011/04/struts-tips.html
Henal Saraiya
RE: Duplicate Submission of Form
July 19, 2011 10:05 AM
Answer

Henal Saraiya

Rank: New Member

Posts: 18

Join Date: July 12, 2011

Recent Posts

Hi Devi,

Well I too encountered the same problem and what solution I applied was to put the server side validation on unique filed (if available) like employee id for every insertion and if user tries to re-insert the value by refreshing the page you can show error message on JSP.

Regards,
Henal Saraiya
Sagar A Vyas
RE: Duplicate Submission of Form
August 6, 2011 12:57 PM
Answer

Sagar A Vyas

Rank: Liferay Master

Posts: 657

Join Date: April 17, 2009

Recent Posts

devi nimmagadda:
Hi,


can anybody know this....

How to prevent duplicate submission of a form ?

Because I am submitting a form with data. When I click on submit button the row is inserted into the database table.

When I refresh the page or go to back button of the same page, The row is inserted again.

Can anybody give suggestion to this.....

Multiple rows are inserting in my table.

Thanks in advance



This is well know problem in Servlet called "Not Idempotent".

Whenever you use actionURL and if it is dealing with your DB then try to build up your business logic in such manner that it should "Idempotent".

Here is solution that I usually use in my business logic hope it will help lot of people.

Step 1)you have to do some stuff in actionURL at your jsp page :

<portlet:actionURL var="addReviewCommentURL" name="myMethod">
<portlet:param name="redirectTo" value="<%=PortalUtil.getCurrentURL(request) %>"></portlet:param>
</portlet:actionURL>


Set one parameter called redirectTo (You can give any name) we are passing current URL.
Step 2) Now at controller side you have do something as below.



@ProcessAction(name = "myMethod")
public void myMethod(ActionRequest actionRequest, ActionResponse actionResponse) throws IOException
{

// some of my business logic
String redirectTo = ParamUtil.getString(actionRequest,"redirectTo");
actionResponse.sendRedirect(redirectTo);

}




Thanks,
Sagar Vyas
Sagar A Vyas
RE: Duplicate Submission of Form
August 6, 2011 12:58 PM
Answer

Sagar A Vyas

Rank: Liferay Master

Posts: 657

Join Date: April 17, 2009

Recent Posts

HI All,

Also want to share why this problem is happen emoticon

Normal scenario is something as below

doView >> Render Default jsp page >> Display view.jsp (or any default jsp)

Client (Browser) send request for some action >> as action fire URL at browser is change >> request come to server >> server will perform operations and send back to response or just status code 200 >>will call doView () >> and render same or other jsp page >> But still Browser is unaware with this it has still old URL (actionURL)
And here problem get start. though DB event done successfully but browser having same URL.

So By Putting My above solution will generate below happy scenario

Client (Browser) send request for some action >> as action fire URL at browser is change >> request come to server >> server will perform operations and send back to response or just status code 200 >> now we send redirect it again via sendRedirect(redirectTo) >> Now again response go to client having old URL and hit change URL of browser as it was before sending actionURL

Hope this help.

Thanks ,
Sagar Vyas
Jitendra Rajput
RE: Duplicate Submission of Form
August 7, 2011 11:04 PM
Answer

Jitendra Rajput

Rank: Liferay Master

Posts: 873

Join Date: January 7, 2011

Recent Posts

Agree with u sagar ...by using sendRedirect() we can avoid duplication problem.
But what if i want to set few renderParameter() before sendRedirect() in that case it will throw error like "render parameter has been already set"..
I am not sure why this problm is coming ..
I had already posted thread but didnt get much information on this
For more information look into

http://www.liferay.com/community/forums/-/message_boards/message/10294343
Jitendra Rajput
RE: Duplicate Submission of Form
August 7, 2011 11:20 PM
Answer

Jitendra Rajput

Rank: Liferay Master

Posts: 873

Join Date: January 7, 2011

Recent Posts

In Liferay 6 you can avoid duplicate refresh by setting <action-url-redirect> parameter in LiferayPortlet.xml ...
It will avoid dupliacte submission of form
Sagar A Vyas
RE: Duplicate Submission of Form
August 8, 2011 12:13 AM
Answer

Sagar A Vyas

Rank: Liferay Master

Posts: 657

Join Date: April 17, 2009

Recent Posts

Jitendra Rajput:
Agree with u sagar ...by using sendRedirect() we can avoid duplication problem.
But what if i want to set few renderParameter() before sendRedirect() in that case it will throw error like "render parameter has been already set"..
I am not sure why this problm is coming ..
I had already posted thread but didnt get much information on this
For more information look into

http://www.liferay.com/community/forums/-/message_boards/message/10294343


Hi Jitendra,

I Have ans your post please check it .
http://www.liferay.com/community/forums/-/message_boards/message/10349345


Ans of
But what if i want to set few renderParameter() before sendRedirect() in that case it will throw error like "render parameter has been already set"..


Consider following scenario :
(1) view.jsp >> (2) some action URL >> (3) doView() >> xyz.jsp(same jsp)

Now you are doing renderParameter() at stage (2) that means you want set parameter which will be accessible at doView() method.

Same thing you can do it with sendRedirect(redirectURL); and set your parameter in redirectURL it self. this will give you your expected output emoticon

It will not refresh page and you will get your parameter in redirectURL (which you suppose generate at your jsp side.)

Thanks,
Sagar Vyas
Chandrasekhar Kothuri
RE: Duplicate Submission of Form
August 15, 2011 10:26 PM
Answer

Chandrasekhar Kothuri

Rank: Regular Member

Posts: 149

Join Date: July 6, 2009

Recent Posts

Hi,

* We have to modify our java to use a redirect instead of a forward
* A forward still submits to the page that it lands on. But a redirect will no longer retain the submit state on the landing page

Comment out the forward line
//setForward(req, "portlet.XXX.XXX.success");

replace with these lines

PortletURL redirectURL = ((ActionResponseImpl) res).createRenderURL();
redirectURL.setParameter("struts_action", "/ext/XXX/success");
res.sendRedirect(redirectURL + "&title=" + XXXTitle);

* Make necessary imports
Phoenix Zerin
RE: Duplicate Submission of Form
August 31, 2011 2:57 PM
Answer

Phoenix Zerin

Rank: Junior Member

Posts: 60

Join Date: June 28, 2011

Recent Posts

Is ActionResponseImpl present in Liferay 6? I seem to be getting NoClassDefFoundError when I use it in my portlet:

21:54:26,538 ERROR [jsp:154] java.lang.NoClassDefFoundError: com/liferay/portlet/ActionResponseImpl
Sagar A Vyas
RE: Duplicate Submission of Form
September 1, 2011 10:58 PM
Answer

Sagar A Vyas

Rank: Liferay Master

Posts: 657

Join Date: April 17, 2009

Recent Posts

Is ActionResponseImpl present in Liferay 6? I seem to be getting NoClassDefFoundError when I use it in my portlet:


Yes this class is available in Liferay 6 at below location.

com.liferay.portlet.ActionResponseImpl

But it is inside the IMPL so you can not use it in your plugins portlet.

Thanks,
Sagar Vyas
Ketan Patel
RE: Duplicate Submission of Form
September 12, 2012 2:37 AM
Answer

Ketan Patel

Rank: Junior Member

Posts: 72

Join Date: May 4, 2012

Recent Posts

Hi!

How to prevent duplicate submission of a form ?


In your liferay-portlet.xml file enter the following line

<action-url-redirect>true</action-url-redirect>
in your portlet tag.

Thanks,
Ketan Patel
http://www.solutionanalysts.com
Cristian Keller
RE: Duplicate Submission of Form
December 20, 2012 5:47 AM
Answer

Cristian Keller

Rank: New Member

Posts: 3

Join Date: January 12, 2012

Recent Posts

We have a problem with double post on update user form with IE7/IE8

We have resolved in this way:

in file edit_user.jsp
We had:
String taglibOnSubmit = renderResponse.getNamespace() + "saveUser('" + ((selUser == null) ? Constants.ADD : Constants.UPDATE) + "');";
...
<aui:form method="post" name="fmpippo" onSubmit="<%= taglibOnSubmit %>">
...
<aui:button type="submit" />

Now, We set:

String taglibOnSubmit = renderResponse.getNamespace() + "saveUser('" + ((selUser == null) ? Constants.ADD : Constants.UPDATE) + "');return false;";
...
<aui:form method="post" name="fmpippo" onSubmit="">
...
<aui:button type="submit" onClick="<%= taglibOnSubmit %>"/>


Also, we have add "return false" on taglibOnSubmit and move the call from form to buttonForm

I dont know if is a correct solution, but in our case this prevent double post submission


Cristian Keller
Shashant Panwar
RE: Duplicate Submission of Form
September 18, 2014 8:03 PM
Answer

Shashant Panwar

Rank: Junior Member

Posts: 28

Join Date: September 21, 2012

Recent Posts

Thanks Ketan, your solution works great with SpringMVC portlets.
"sendRedirect" does not work with SpringMVC portlets due to SpringMapping Interceptor.

Regards,
Shashant