Play framework is joining the Typesafe Stack — More information in the official announcement

Meet the community

Code snippets

You can submit your code snippets here, to share patterns with the community. The content is uncensored, unfiltered and represents the views of the individual community members, not official Play usage. To contribute just login with your OpenID.

Consume Google spell check API with Scala

Spell checking a text may seem complicated. But for each complicated problem there exists a Web service API. Integrating it using Scala and Play is trivial.

Show the code

Sample Secure interceptor

How to create a Secure interceptor. The same principle is applied in the secure module.

Show the code

Bidirectional OneToMany+Yaml

This shows two simple classes linked bidirectionally by many to one and one to many relationship and a yaml file which loads sample data for each in.

Show the code

Upload and store image

Simply shows how store uploaded images and display them later.

Show the code

Keep track of JPA record creation/updates

Ever wanted to keep track of record creation and update timestamps ?

The solution presented is an easy way around the use of DB triggers. It’s datavase agnostic and completely transparent to the developer: The JPA layer takes care of everything using @post hooks.

Show the code

Consume JSON using a GsonBinder

Using the new custom Binder of Play 1.1, you can automatically transform the request body into a Gson element.

Then you can extract the data ‘manually’ or use the Gson binding capabilities to transform it automatically into your domain object type.

The code snippet show a JsonObject binder, but you can of course do the same for other types like JsonArray.

Show the code

Run Sample LinkedIn OAuth App

Download and Install Play! 1.1.1, Clone and Run Sample Integration App with LinkedIn OAuth

Show the code

Authenticate with Twitter

This snippet shows how to authenticate with Twitter using Scribe. I’ve split the code up into three classes:
1. A Model to hold Twitter related stuff
2. A Controller
3. A Library class holding all the Twitter funcionality. It includes a couple of API methods you can try out

Show the code

URL Encode/Decode Tags

Tags for encoding and decoding URLs in the template engine.

Show the code

Demonstrate a Job progress status solution for Playframework

How can I keep track of a job progress ?

Playframework does not provides a solution for tracking a Job progression, it’s the concern of the programmer.

So here a simple example done after reading the Play! GoogleGroup. It use the Cache (play.cache.Cache) and the UUID (play.libs.Codec).

Show the code

Alternative to Play! Blob for File Upload

This is a proposition for an alternative to the File Upload in Play! Framework (> 1.2). This aims to replace Blob for serving uploaded files through a Static Web Server.

Show the code

init.d script for Play

CentOS/Redhat init.d script to launch Play as a service.

Instalation:
copy file to /etc/init.d
chmod +x /etc/init.d/play
chkconfig --add /etc/init.d/play
chkconfig play on

Usage: (as root)
service play start
service play stop
service play status

Show the code

Change database schema at runtime

I was working with a Postgres DB with multiple schemas and I needed to be able to switch from one schema to another on the fly. This little hack allows you to do that.

Show the code

Using jasper reports with play framework

Here simple code which allow you to generate jasper report from play framework.

Show the code

playframework .gitignore template file

Template .gitignore file for playframework applications.

credits to https://github.com/github/gitignore/blob/master/PlayFramework.gitignore

Show the code

A simple file upload and renderBinary example using Play! 1.2.3 and GAE (google appengine) 1.5.6

A simple file upload and renderBinary example using Play! 1.2.3 and GAE (google appengine) 1.5.6. Couldn’t find a complete (working) example of html file upload and renderBinary using Play! framework on GAE and then spent some time to get it working. Here it is

Show the code

(Ubuntu) Upstart Script for Playframework 1.x Apps

This is a upstart script for Play 1.x apps.

it features:
- boots on server boot
- automatically respawning of the app if it goes down
- runs the app as unprivileged user
- sets the working directory to the apps root directory ( so a simple new File(“public/images/myImage.png”) would work)
- specifying the profile id for the app to be run

have fun!

p.s.: if you have improvements, please fork the gist and drop me a line at

${me.firstname}@${me.firstname}${me.lastname}.com

Member(me):
firstname: dominik
lastname: dorn

;) Show the code

Gzip your response

Simply gzips your applications' responses.

Show the code

Uniqueness validation

Introduction

A (very basic) annotation and check. It expects that your model extends play.db.jpa.Model and also have the following limitations:

h3. How to use it

Just annotate the attribute like that:

@Unique
public String email;

I also recomend that you put a @Column annotation that creates a constraint at database:

@Unique
@Column ( unique = true )
public String email;

The @Unique annotation also permits empty/null values, so, you should probably use @Required annotation as well:

@Unique @Required @Column( unique = true )
public String email;

What else

Fell free to fork the gist and customize the code for you necessities.

Show the code

Specify another location for the h2 file database

By default, when db=fs is specified play stores the h2 database file at db/h2/play with this setting you can specify another location for the h2 file database.

More info at h2 documentation site

Show the code

Pagination

This is very basic pagination functionality, completely agnostic of the underlying data.

Code + Example Usage:
https://gist.github.com/1379217

The code is based on this mailing list discussion:
http://groups.google.com/group/play-framework/browse_thread/thread/32f9e6f5ff6152a6?pli=1

Show the code

Play2.0 Facebook Login

A gist to add Facebook login to your application

Show the code

Play! Framework 1.2.x Pagination

Play! Framework 1.2.x Pagination (this is a variation and modification of the version found on https://gist.github.com/1379217). This gist includes all the code you need to setup paging in your views minus the CSS to style the paging links.

Show the code

Fluently build Play! routes

Demonstrates how to easily and fluently build routes from within Controller methods.

Check out the usage below.

Show the code

nexus.py

Play command for pushing SNAPSHOT releases into Nexus.
Currently this is just what we need but it would be really easy to include ‘releases’ support.

Usage.

play nexus-commit [-a]

-a: push the file without asking for confirmation (always)

Show the code