Skip to content

play 2.0 in scala ide 2.0

skyluc edited this page Jan 8, 2012 · 26 revisions

Setup and use Play framework 2.0 in Scala IDE 2.0

This guide has been integrated in the Scala IDE doc

Hi all, thanks for check-in out this guide. Its master version has been moved to the Scala IDE documentation project on GitHub and is availble in the new Scala IDE website http://scala-ide.org/docs/tutorials/play20scalaide20/index.html.

For comments or improvements, use GitHub ticket and pull request system.

Luc Bourlier - +Luc Bourlier @sky1uc

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 a simple example of a change in the web application.

Prerequisites

[luc] links to installation instructions

  • A basic knowledge of the Eclipse user interface is required.

  • No knowledge of the Scala language is required (in this guide).

  • No knowledge of the Play framework is required (in this guide).

Setting up Play 2.0

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.0-beta from http://www.playframework.org/2.0.

  • Unzip it in your preferred location. Let's say /path/to/play20 for the purpose of this document.

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

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

Creating a Play 2.0 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.0 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.

The eclipsify support is not yet integrated it Play 2.0, so we are using sbteclipse to make it an Eclipse project.

  • First, exit Play using ctrl-d, and then exit.

    ctrl-d, exit

  • Add sbteclipse to sbt by creating the project/build.sbt file and adding the following lines.

resolvers += Classpaths.typesafeResolver

addSbtPlugin("com.typesafe.sbteclipse" % "sbteclipse" % "1.5.0")
![modify project/build.sbt](scala-ide/images/play20-scalaide20-07.png)
  • Go back into Play.

    play

  • Generate the Eclipse project configuration.

    eclipse

  • And relaunch the web application, so it is available later.

    run

Configuring Scala IDE for the Play 2.0 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 filesystem are automatically picked up.

    refresh automatically

  • If you don't have the Web Development Tools for Eclipse installed, Eclipse opens .html files in a web browser. Configure it to use the Scala Editor instead.

    html file in text editor

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.0 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 change the content. Let's add a way to have a quote on the main page.

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

    create model.Quote

  • Add variables to models.Quote.

package models

case class Quote(val text: String, val 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.0 beta") {

    @play20.welcome(message)

    <p>@quote.text<em> - @quote.author</em></p>

}
  • The templates are transformed into Scala code by the Play framework, so use the refresh button in the internal web browser to trigger it.

    Play returns a compilation error, the application is not using the template correctly. The error is also visible in the code of Application.scala.

    compilation error

  • Fix the application code, using a smart quote.

  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.0 and scala.

For more information about Play 2.0, check out the Play 2.0 wiki.

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

Feedback

This guide now lives in the Scala IDE doc project on github. Please use github ticket and pull request system for feedback.

Luc Bourlier - +Luc Bourlier @sky1uc