If you're interested in functional programming, you might also want to checkout my second blog which i'm actively working on!!
Showing posts with label playframework. Show all posts
Showing posts with label playframework. Show all posts

Monday, April 15, 2013

Using Bootstrap with Play 2

The easiest way to get bootstrap working with Play 2 is not to separate the bootstrap files in the corresponding stylesheets, javascripts and images folders but just drop the sources in like shown below.

Loading ....

Sunday, December 23, 2012

Getting Play2.0.x to work with Git Bash on Windows

I unzipped play2.0.4 and created a PLAY_HOME variable. I also made sure to add PLAY_HOME to my PATH variable.

nxp10009@NXL01366 /c/development/play-2.0.4
$ echo $PLAY_HOME
C:\development\play-2.0.4

When I tried to run play from the Git Bash shell I ran into below exception. In the past I didn't bother that much and just used the DOS shell as a workaround. But I like to work from one shell only so time to look for a solution.
nxp10009@NXL01366 /c/workspaces/scala
$ play
Error during sbt execution: Could not find configuration file 'c:/development/play-2.0.4/framework/sbt/play.boot.properties'.  Searched:
        file:/c:/workspaces/scala/
        file:/C:/Users/nxp10009/
        file:/C:/development/play-2.0.4/framework/sbt/

Luckily someone created a patch to resolve this issue. You can download the gist and unzip it to some folder. It will contain a file named play_cgywin.patch. So it's matter of applying the patch from within Git Bash. It will log an error but play will work nonetheless.
nxp10009@NXL01366 /c/development/play-2.0.4
$ patch -p1 < c:/tmp/play_cygwin.patch
patching file `framework/build'
Hunk #1 FAILED at 8.
1 out of 1 hunk FAILED -- saving rejects to framework/build.rej
patching file `play'


nxp10009@NXL01366 /c/workspaces/scala
$ play
Getting play console_2.9.1 2.0.4 ...
:: retrieving :: org.scala-sbt#boot-app
        confs: [default]
        5 artifacts copied, 0 already retrieved (3667kB/83ms)
       _            _
 _ __ | | __ _ _  _| |
| '_ \| |/ _' | || |_|
|  __/|_|\____|\__ (_)
|_|            |__/

play! 2.0.4, http://www.playframework.org

Friday, October 5, 2012

Using Play JSON library to expose JSON services

Currently I am experimenting with Playframework. Today we will see how easy it is to generate a JSON response. Let us first take a look at our domain model
package models

case class Contact(firstName: String, lastName: String, age: Int)

object Contact {
  val contacts = Set(
      Contact("Robby", "Pelssers", 35),
      Contact("Davy", "Pelssers", 35),
      Contact("Lindsey", "Pelssers", 9)
  )
  
  def findAll = this.contacts.toList.sortBy(_.firstName)

}
Next let us take a look at a simple controller returning contacts in JSON format
package controllers

import play.api.mvc.{ Action, Controller }
import play.api.libs.json.Json._
import models.Contact

object Contacts extends Controller {

  def toJSON = Action { implicit request =>
    val contacts = Contact.findAll   
    val json = 
      toJson(
        contacts.map(
            contact => toJson(
                Map("firstName" -> toJson(contact.firstName), 
                    "lastName" -> toJson(contact.lastName), 
                    "age" -> toJson(contact.age))
            )    
        )
      )
     
    Ok(json)
  }
  
}

Now it's a matter of mapping a URL to our controller method.
GET     /contacts.json              controllers.Contacts.toJSON
Now let's see if our response looks ok by using curl
$ curl --request GET --include http://localhost:9000/contacts.json
HTTP/1.1 200 OK
Content-Type: application/json; charset=utf-8
Content-Length: 160

[{"firstName":"Davy","lastName":"Pelssers","age":35},{"firstName":"Lindsey","lastName":"Pelssers","age":9},{"firstName":"Robby","lastName":"Pelssers","age":35}]