Setup and use Play framework 2.1 in Scala IDE 3.0

What is in this guide?

This guide will show you how to configure a Play web application to import it in Scala IDE, how to configure Scala IDE to work fine with the Play framework and finally how to develop Play web application from inside Scala IDE.

Prerequisites

Installing the Scala IDE Play2 plug-in

The Scala IDE Play2 plug-in provides first-class support for Play2 Route and Template files in Eclipse, for both Java and Scala projects. In practice, what this means is that many of the features your are used to having in the Scala or Java Editor (e.g., syntax highlighting, errors-as-you-type reporting, code completion, hyperlinking) are also available when editing Play2 files.

The Play2 plug-in can be installed via the same Eclipse update-site that you used to install the Scala IDE. The Play2 plug-in is listed under the Scala IDE plugins group.

install Play2 plug-in

For a comprehensive review of the main features available in the Play2 plug-in, please read the here.

Setting up Play 2.1

To be able to create a Play web application, the Play framework need to be installed. If you have not installed it already, follow this few steps, or use the Play documentation.

  • Download Play framework 2.1 from http://www.playframework.org/.

  • Unzip it in your preferred location. Let’s say /path/to/play21 for the purpose of this document. (from the Play documentation: Running play writes some files to directories within the archive, so don’t install to /opt , /usr/local or anywhere else you’d need special permission to write to.)

  • For convenience, add the Play folder to your system PATH:

    export PATH=$PATH:/path/to/play21
    

Creating a Play 2.1 application

  • In your development folder, ask Play to create a new web application, as a simple Scala application.

    play new testApp
  • Go into the application folder.

    cd testApp
  • And launch Play.

    play
  • In Play, launch your newly created web application.

    run
  • Check that the application works: http://localhost:9000/.

    running

Configuring the Play 2.1 web application for Scala IDE

Now that the Play application is running, it needs to be configured so it can be imported into Scala IDE.

Play 2.1 integrates sbteclipse, which allow to create configuration files of a project for Eclipse.

  • First, exit the ‘run’ mode in Play using ctrl-d.

    ctrl-d, exit
  • eclipse is the command to invoke sbteclipse in Play. (or eclipse with-source=true if you want to also download sources attachment of your dependencies)

    eclipse
  • Relaunch the web application, in ‘auto-reloading’ mode, using ~ run, so it is running in the background.

    run

Configuring Scala IDE for the Play 2.1 web application

Setting a few preferences in Eclipse will make everything easier to use.

  • Open the internal web browser view in Eclipse, and check you can access your web application.

    http://localhost:9000/
  • Configure Eclipse so changes on the file system are automatically picked up.

    refresh automatically

Importing the Play web application into Scala IDE

Everything is setup, it is time to import the project in the IDE.

  • Import the Play 2.1 application as an Existing Projects into Workspace.

    import project
  • Everything is good, everything compiles.

    everything compiles

Doing some development

Now that everything is setup, we can start to do some real work.

Let’s change the main page to display a quote instead of the default page.

  • First, create the models.Quote class using the new Scala Class wizard.

    create model.Quote
  • Add variables to models.Quote, and make it a case class.

    package models
    
    case class Quote(text: String, author: String)
    
  • Add an extra parameter to the index.scala.html view and update the layout.

    @(message: String, quote: models.Quote)
    
    @main("Welcome to Play 2.1") {
    
        <p>@quote.text<em> - @quote.author</em></p>
    
    }
    
  • The templates are transformed into Scala code by the Play framework. As Play has been started in auto-reloading mode in the background, templates are recompiled as soon as the file is saved.

    After saving the file, the changes are picked up by Scala IDE, and it reports an error in the code of Application.scala. The application is not using the template correctly.

    compilation error
  • Fix the application code, using a smart quote. And fix the imports as needed.

    def index = Action {
      Ok(views.html.index("Your new application is ready.",
          Quote("Citer les pensees des autres, c'est regretter de ne pas les avoir trouvees soi-meme.",
              "Sacha Guitry")))
    }
    
  • The code compiles. Check the result in the internal web browser.

    done

Going further

You now have all you need to create great web applications with Play 2.1 and Scala.

For more information about Play 2.1, check out the embedded documentation.

For more information about Scala, go to the documentation website or get the downloadable eBook.

Feedback

This guide is managed through in the Scala IDE documentation project on github. Please use github tickets and pull requests system for feedback.

Luc Bourlier - +Luc Bourlier @sky1uc