CHAPTER 8: Play with Java and Scala 371
Now you can run the helloworld-scala project using the run command from the Play console, as
illustrated in Figure 8-19.
Figure 8-19. Play console for helloworld-scala application
Now you will look at the controller generated by Play 2 for helloworld-scala (see Listing 8-4). You
can find the controller in helloworld-scala\app\controllers.
Listing 8-4. Application Controller in Scala
- package controllers
- import play.api._
- import play.api.mvc._
- object Application extends Controller {
- def index = Action {
- Ok(views.html.index("Your new application is ready."))
- }
- }
Line 6: As you can see on line 6, in Java controller is a class, but in Scala the
controller is an object.
Line 8: As you can see on line 8, in Java action is a static method, but in Scala
an action is a function (an object’s method).
Line 9: The return type and keyword are missing if you compare this to the Java
controller.
Line 9: Scala uses a structure called Action, which is a block of code executor.
Now that you have seen the controller in Scala, which differs from a Java controller syntactically,
it is time to see the template in helloworld-scala (see Listing 8-5), which you can find in
helloworld-scala\app\views.