user1980875
user1980875

Reputation:

How to implement a POST-REDIRECT-GET in Play Framework

Let's say I have two controller methods: Users.preInsert and Users.insert. The preInsert method is the one used to display the user entry form (GET), while the insert method is responsible for the actual insertion (POST) or calling the 'insert' service. This is how the routes looks like:

GET    /users/add                           controllers.Users.preInsert(...)
POST    /users/add                           controllers.Users.insert(...)

So how do I redirect a request (POST to GET) without losing the parameters like error messages returned from the insert service and the values inputed by the client so that they can be accessed and displayed in the entry form. The parameters may involve some complex objects. I have implemented it using the Caching API but I would like to know if there are any better ways of doing it.

Upvotes: 6

Views: 2685

Answers (2)

biesior
biesior

Reputation: 55798

You don't need to redirect it back to the preInsert action, instead at the beginning of the insert check if form has errors and it it has display your view containing form (the same which you used in preInsert). It's described in the doc mentioned by nico_ekito in section Handling binding failure

Upvotes: 0

ndeverge
ndeverge

Reputation: 21564

That's the exact purpose of the Form objects (http://www.playframework.com/documentation/2.1.1/ScalaForms).

And I think there is a an error in your routes, it could look like:

GET    /users/add                           controllers.Users.preInsert(...)
POST   /users/add                           controllers.Users.insert(...)

You should definitively take a look at the form sample.

Upvotes: 3

Related Questions