Play applications use controllers to handle HTTP requests and responses. Play controllers are composed of actions that have specific functionality. Play applications use a router to map HTTP requests to controller actions.
To create a new page, which prints out "Hello World" for a Play Java project, we need to take the following steps:
Run the
foo_java
application with Hot-Reloading enabled:$ activator "~run"
Edit
foo_java/app/controllers/Application.java
by adding the following action:public static Result hello() { return ok("Hello World"); }
Edit
foo_java/conf/routes
by adding the following line:GET /hello controllers.Application.hello()
View your new hello page using a web browser:
http://localhost:9000/hello
For Scala, we need to take the following steps:
Run the
foo_scala
application with Hot-Reloading enabled:$ activator "~run"
Edit
foo_scala/app/controllers/Application.scala
by adding the following action:def hello = Action { Ok("Hello World") }
Edit
foo_scala/conf/routes
by adding the following line:GET /hello controllers.Application.hello
View your new hello page using a web browser:
http://localhost:9000/hello
In this recipe, we enumerated the steps necessary to create a new accessible page by creating a new web action in a controller and defined this new page's URL route by adding a new entry to the conf/routes
file. We should now have a "Hello World" page, and all without having to reload the application server.