background preloader

Play! Framework

Facebook Twitter

Introducing the Play Framework. YAML. YAML syntax Your application tests will often use data structures that consist of related model objects, which can be tedious to create programmatically in Java.


Play includes a YAML parser which makes the task much simpler. Create a YAML file like the following, in the conf directory. City(capitalfederal): name: Capital Federal state: Buenos Aires User(john): name: "John Smith" email: lang: es created: 2007-01-01 User(frank): name: "Frank Franken" email: lang: en created: 2007-08-05 Neighborhood(villalugano): name: Villa Lugano city: capitalfederal tags: "shopping, cafes" users: [john, frank] Neighborhood(coghlan): name: Coghlan city: capitalfederal tags: "cafes, bars" users: [john] Play’s YAML syntax varies slightly from the standard. Play uses SnakeYAML, which has an excellent documentation page with many examples. Snakeyaml. This documentation is very brief and incomplete.


Feel free to fix or improve it. Installation If you use Maven just add a dependency as described here. If you do not use Maven download the latest JAR and put it to the classpath. Frequently Asked Questions Dictionaries without nested collections are not dumped correctly Why does. Play Framework - Better JSON serialization with FlexJSON. We want to be able to create multiple different JSON views on the same object, to be used in our Play templates.

Play Framework - Better JSON serialization with FlexJSON

This is useful, because entities are often used in several contexts. A news item may be shown with only a few details in a listing of news items, with more details on a separate page for that news item, and with even more details for an editor of the website. When you use Gson, the default JSON representation of a class is a map with key-value pairs that represent the field names and their values. Serialization is done recursively, which poses the first problem, namely that you cannot serialize objects that have circular references. Declarative model class enhancement in Play. In summary, this article describes a simple example of how to define a Java annotation that will declaratively add behaviour to a model class in a Play framework application.

Declarative model class enhancement in Play

You can do with with a custom annotation and a Play module that enhances the model JavaBean class. Suppose your Play application uses the CRUD module to provide a default user-interface for managing data such as the following JavaBean. public class User extends Model { public String name; public String email; public String address; } The CRUD module's default admin page lists the users like this: Textile. Play! Framework cheat sheet. Play framework - Setting up a basic administration area using CRUD. Currently, we have no way to create new blog posts or moderate comments using the blog UI.

Play framework - Setting up a basic administration area using CRUD

Play provides an out of the box CRUD module that will help quickly generate a basic administration area. Enabling the CRUD module. Overview (Hibernate JavaDocs) Closure (Groovy 1.7.5) Google Group. Caching. Ehcache API. Spymemcached - java memcached interface. A simple, asynchronous, single-threaded memcached client written in java.

spymemcached - java memcached interface

Efficient storage of objects. General serializable objects are stored in their serialized form and optionally compressed if they meet criteria. Certain native objects are stored as tightly as possible (for example, a Date object generally consumes six bytes, and a Long can be anywhere from zero to eight bytes). Javadocs for spymemcached 2.4.2. Memcached. Module basics. A Play application can be assembled from several application modules.

Module basics

This allows you to reuse application components across several applications or split a large application into several smaller applications. What is a module? A module is just another Play application; however some differences exist in the way resources are loaded for an application module: UnboundID LDAP API. Using UnboundID LDAP SDK. LDAP SDK for Java Using the UnboundID LDAP SDK Persistence Framework.

Using UnboundID LDAP SDK

LDAP Connection Pools with StartTLS. Com.unboundid.ldap.sdk Class StartTLSPostConnectProcessor java.lang.Object com.unboundid.ldap.sdk.StartTLSPostConnectProcessor.

LDAP Connection Pools with StartTLS

Javassist API. Javassist Tutorial. Getting Started with Javassist Shigeru Chiba.

Javassist Tutorial

YAML Quickstart. YAML Reference. Logging configuration. The Play logger is built on Log4j. Since most Java libraries use Log4j or a wrapper able to use Log4j as a backend, you can easily configure logging that is well-suited to your application. Log4j 1.2.16 API. Java extensions. Java 6 API. Deployment. Play applications can be deployed virtually anywhere: inside Servlet containers, as standalone servers, in Google Application Engine, Stack, a Cloud, etc... Standalone Play applications The simplest and the more robust way is to simply run your Play application without any container. You can use a frontal HTTP server like Lighttpd or Apache if you need more advanced HTTP features like virtual hosting.

The built-in HTTP server can serve thousands of HTTP requests per second so it will never be the performance bottleneck. Moreover it uses a more efficient threading model (where a Servlet container uses 1 thread per request). Those servers support long polling and allow to manage very long requests (waiting for a long task to complete), and direct streaming of File objects (and any InputStream if you specify the Content-Length), without blocking the execution thread. GlassFish.

Google App Engine. Gae module. Moving to production. Here some simple tips to optimize your application for production. Configure your application.conf First off, the best way to specify production mode is to give a specific ID to your production framework. Let’s pick production as an example. Testing. Creating automatic test suites for your application is a good way to make it robust. It allows you to work in a very agile way. Play tests are built using JUnit 4 or Selenium depending what you want to test. Writing tests The tests must be created in the test/ directory. This folder will only be added to the sources path when the application is run in test mode.

Unit test. Selenium Documentation. ReST. Representational State Transfer (REST) is a software architecture style consisting of guidelines and best practices for creating scalable web services.[1][2] REST is a coordinated set of constraints applied to the design of components in a distributed hypermedia system that can lead to a more performant and maintainable architecture.[3] REST has gained widespread acceptance across the Web[citation needed] as a simpler alternative to SOAP and WSDL-based Web services. RESTful systems typically, but not always, communicate over the Hypertext Transfer Protocol with the same HTTP verbs (GET, POST, PUT, DELETE, etc.) used by web browsers to retrieve web pages and send data to remote servers.[3] The REST architectural style was developed by W3C Technical Architecture Group (TAG) in parallel with HTTP 1.1, based on the existing design of HTTP 1.0.[4] The World Wide Web represents the largest implementation of a system conforming to the REST architectural style.

Architectural properties[edit] How Comet works in Play! Comet. Comet is a web application model in which a long-held HTTP request allows a web server to push data to a browser, without the browser explicitly requesting it.[1][2] Comet is an umbrella term, encompassing multiple techniques for achieving this interaction. All these methods rely on features included by default in browsers, such as JavaScript, rather than on non-default plugins.

The Comet approach differs from the original model of the web, in which a browser requests a complete web page at a time.[3] The use of Comet techniques in web development predates the use of the word Comet as a neologism for the collective techniques. Comet is known by several other names, including Ajax Push,[4][5] Reverse Ajax,[6] Two-way-web,[7] HTTP Streaming,[7] and HTTP server push[8] among others.[9] Even if not yet known by that name, the very first Comet implementations date back to 2000,[18] with the Pushlets, Lightstreamer, and KnowNow projects.

Models. JPA Annotations. Template syntax. Play has an efficient templating system which allows to dynamically generate HTML, XML, JSON or any text-based formatted document. The template engine uses Groovy as an expression language. Template tags. These are the built-in tags that are available in addition to the core template engine syntax. Implicit Objects in templates. Play has an efficient templating system which allows to dynamically generate HTML, XML, JSON or any text-based formatted document. Controllers. Implicit Objects in Controllers. RenderJson. Gson User Guide. Serializing and Deserializing Collection with Objects of Arbitrary Types Sometimes you are dealing with JSON array that contains mixed types. GSON API. Play! API. Getting Started. Dev Roadmap. Submitting Patches. Coding style guide. Learning Scala. Scala module documentation. Getting Started Guide. OVal 1.61 API. OVal - Object Validation. jQuery API.

JavaScript Micro-Templating. jQuery UI.