Actions
An Action in Play defines how a server should respond to a request. The methods, which define an Action, are mapped to a request in the routes
file. For example, let's define an Action which displays the information of all the artists as a response:
def listArtist = Action { Ok(views.html.home(Artist.fetch)) }
Now, to use this Action, we should map it to a request in the routes
file.
GET /api/artist controllers.Application.listArtist
In this example, we fetch all the artists and send them with the view, as the response to the request.
Note
The term api
used in the route
file is just a URL prefix and is not mandatory.
Run the application and access http://localhost:9000/api/artist
from the browser. A table with the available artist is visible.
Action takes a request and yields a result. It is an implementation of the EssentialAction
trait. It is defined as:
trait EssentialAction extends (RequestHeader => Iteratee[Array[Byte], Result]) with Handler { def apply() = this ...