I’m just learning how the Play 2.0 framework. So I have a quite basic question: I just want to take a URL parameter and display it in the view. How do you do that?
I created the URL pattern:
GET /test/:id controllers.Application.testOutput(id: Long)
And an apporoptiate methode in Application:
public static Result testOutput(long id) {
return ok(
views.html.test.render(id)
);
}
How do I call the id variable form the view? I know how to call methodes defined in the model in the view, but I don’t know how to display the id variable in the view. Is it the right way to pass the id variable to the render methode?
I’d like to understand the underlying concept, so an detailed explanation to the answer would be great!
Our test URL will be
http://localhost:9000/greeter?message=helloand this will output atext/plainresponse with the content of the parametermessage(ie hello). First, let’s define the routeThen, create a
Greetercontroller (I use Java)You can see that
ok()calls a scala function defined in the fileapp/views/greeter.scala.txtHere is the content of that file (the first line defines the message parameter of type String used inside the functionIn this case I used .txt for file extensions because I wanted plain text response. If you want to produce HTML output, simply make a .scala.html file