Post-Redirect-Get Pattern in Spring MVC

Do you have back button issues? Duplicate form submissions when you click back button? I think you have landed in right blog. With respect to Spring MVC, the blog even tries to give the complete solution by which to handle it.

One of the easiest way by which you can handle these issues is by implementing Post-Redirect-Get (PRG) pattern.

What is Post-Redirect-Get Pattern?

Post/Redirect/Get (PRG) is a web development design pattern that prevents duplicate form submission. When a web form is submitted to a server through an HTTP POST request, a web user that attempts to refresh the server response in certain user agents can cause the contents of the original HTTP POST request to be resubmitted, possibly causing undesired results, such as a duplicate web purchase.

To avoid this problem, PRG pattern is used, instead of returning a web page directly, the POST operation returns a redirection command. The HTTP 1.1 specification introduced the HTTP 303 response code to ensure that in this situation, the web user’s browser can safely refresh the server response without causing the initial HTTP POST request to be resubmitted.

I don’t want to re-invent the wheel, so i would just let the reader go through the Wikipedia which has explained it beautifully. Please follow the following URL:

Beautiful well constructed figures in Wikipedia will make you understand this pattern in minutes.

I hope you would now have a clear picture of PRG pattern after going through the above URL and are ready to see this implemented in your Spring MVC application as detailed below in this blog post.

I really don’t know whether this is the best approach by which Post-Redirect-Get pattern can be achieved in Spring MVC. Nevertheless i am sharing an approach below by which PRG pattern can be achieved. If this is not at all right approach, or there is another good way by which to do it, please let me know through your comments to this blog post and i promise to correct it accordingly.

In Spring MVC PRG is implemented by having two request-mappings for each page one for GET and the other for POST. The page should always be rendered using GET method and the form should always use POST method for submitting data to controller. To achieve PRG after submitting the form in the POST method of the request should send the response with redirect to the page, by doing this the page will be rendered using GET request. The following a sample code.

The flow of the above example is {GET(prgTest.html} –> {POST(prgTest.html)} –> {REDIRECT(prgTest.html)} –> {GET(prgTest.html)}

I thank my dear friend Karthikeyan Vaithilingam, for allowing me to print his R&D on this in my blog. Please follow the following link to his other blog entries:

I would also suggest reader to go through another approach as given in the below blog post and implement whichever is the best approach in your web application.

If you are using JSF or one its implementations like RichFaces as your choice of web application, to implement PRG, please blog my blog in here.

Page Visitors: 11475

The following two tabs change content below.
Tomcy John

Tomcy John

Blogger & Author at javacodebook
He is an Enterprise Java Specialist holding a degree in Engineering (B-Tech) with over 10 years of experience in several industries. He's currently the Senior Technical Engineer at Emirates Group IT since 2005. Prior to this he has worked with Oracle Corporation and Ernst & Young. His main specialization is on various web technologies and acts as chief mentor and Principal Architect to facilitate incorporating Spring as Corporate Standard in the organization.

2 thoughts on “Post-Redirect-Get Pattern in Spring MVC

Leave a Reply

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