Learn Java for Web Development

(Tina Meador) #1
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



  1. package controllers



  2. import play.api._

  3. import play.api.mvc._



  4. object Application extends Controller {



  5. def index = Action {

  6. Ok(views.html.index("Your new application is ready."))

  7. }



  8. }


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

Free download pdf