Search code examples
scalaplayframework-2.0post-redirect-get

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.


Solution

  • 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.