About this Book

You’re probably reading this book because you want to build a web app. This book is about one way of doing that.

There are so many different web applications that the question, “How should I do X?” can often only be answered with, “It depends.” So instead of trying to give some general advice that won’t be good for many cases anyway, we’ll introduce Play’s components, their relations, and their strengths and weaknesses. Armed with this knowledge, and the knowledge of your project that only you have, you can decide when to use a tool from Play or when to use something else.

In this book we use a fictitious company managing paperclip logistics as a vehicle for example code. This isn’t one running example that gets bigger with each chapter, culminating in a complete application at the end of the book. Rather, we wanted to save you from the cognitive load of having to “get into” the business domain of many different examples, so we chose this as a common business domain. The examples and the chapters themselves are mostly standalone, to aid readers who don’t read the book in one go or who want to skip chapters. We understand that some readers would value building one application that uses concepts from multiple chapters while reading the book, and we encourage those readers to pick a more interesting problem than that of paperclip logistics, and to try to adapt what they learn from this book to solving that problem instead.

The web entails many more technologies than any book could possibly encompass. We focus on Play and the boundaries between Play and other technologies, but not more. We expect that the reader has a basic understanding of the web in general and HTTP and HTML in particular.

This isn’t a book about learning Scala, although we understand that Scala is likely new to many readers as well. We recommend picking up this book after an introduction to Scala, or in parallel with an introduction to Scala. Though we stay clear of the hard parts of Scala, some of the language constructs will likely be hard to grasp for readers who are entirely unfamiliar with Scala.

This book isn’t the one book about Play that covers everything. Partly, this is because Play is a new framework and is evolving rapidly. Best practices are often not worked out yet by the Play community. There’s also a more mundane reason: page count. The subject of testing, for example, didn’t fit within the page limit for the book, and rather than doing a very condensed chapter about testing, we chose to leave it out.

If you’re curious, the short version is that Play is highly testable. This is partly due to its stateless API and functional style, which make the components easier to test. In addition, there are built-in testing helpers that let you mock the Play runtime and check the results of executing controller actions and rendering templates without using HTTP, plus FluentLenium integration for user-interface level tests.

Rather than trying to cover everything, this book tries to lay a foundation, and we hope that many more books about Play will be written. There’s much to explore within Play and on the boundaries between Play and the Scala language.

Roadmap

Chapter 1 introduces the Play framework, its origins, and its key features. We look at how to get started with Play, and glance over the components of every Play application.

Chapter 2 shows in more detail the components of a Play application and how they relate to each other. We build a full application with all the layers of a Play application, with multiple pages, and with validation of user input.

Chapter 3 starts with a dive into the architecture of Play. We show why Play works so well with the web, and how control flows through your application. We look at how the models, views, and controllers of an application fit together and how an application can be modularized.

Chapter 4 focuses on controllers. Controllers form the boundary between HTTP and Play. We see how to configure a Play application’s URLs, and how to deal with URL and query string parameters in a type-safe way. We use Play forms to validate and retrieve user input from HTML forms, and we learn how to return an HTTP response to the client.

Chapter 5 shows how a persistence layer fits into a Play application. Anorm is a data access layer for SQL databases that’s bundled with Play and works with plain SQL. As a possible alternative, we also introduce Squeryl, which is a data access layer that uses a Scala domain-specific language to query a database.

Chapter 6 shows how Play’s template engine works. It discusses the syntax and how the template engine works together with Scala. We see how we can make reusable building blocks with templates and how to compose these reusable blocks to construct larger templates.

Chapter 7 goes into more detail on the subject of Play forms. Forms are a powerful way to validate user data, and to map data from incoming HTTP requests to objects in Scala code. They also work in the other direction: they can present Scala objects to a user in an HTML form. We also learn how to create forms for complex objects.

Chapter 8 introduces Play’s JSON API in the context of a sample application with a JavaScript front end that uses the Play application as a web service. Play’s JSON API assists with converting JSON to Scala objects and generating JSON from Scala objects.

Chapter 9 focuses on Play in a bigger context. We see how we can use existing Play modules and how to create our own modules and plugins. We glance over the various ways to deploy an application and how to deal with multiple configurations effectively.

Chapter 10 starts with a description of Play’s web service API and how you can leverage it to consume the APIs of other web applications. The second part of this chapter introduces more advanced concepts of Play, such as iteratees, a Play library that helps you work with streams of data and WebSockets.

Code conventions and downloads

All source code in the book is in a fixed-width font like this, which sets it off from the surrounding text. This book contains many code listings to explain concepts and show particular Play APIs. The listings don’t always result in a full application; other code that’s outside the scope of the chapter is also needed. In many listings, the code is annotated to point out the key concepts.

The code in this book is for Play versions 2.1.x, which is the most recent version of Play at the time of printing. If you are using a different version of Play, some of the code details might be different.

For your convenience, we’ve put up complete example applications for all chapters on GitHub: https://github.com/playforscala/sample-applications. These applications are available for multiple versions of Play, organized in a branch named to the Play version. The source code is also available for download from the publisher’s website at www.manning.com/PlayforScala.

The code in these applications isn’t identical to the listings in this book; often things from multiple listings are merged in the complete application. Some additional HTML markup, which would obfuscate the main point of a listing in the book, is used in some places for aesthetic reasons.

Author Online

Purchase of Play for Scala includes free access to a private web forum run by Manning Publications where you can make comments about the book, ask technical questions, and receive help from the authors and from other users. To access the forum and subscribe to it, point your web browser to www.manning.com/PlayforScala. This page provides information on how to get on the forum once you’re registered, what kind of help is available, and the rules of conduct on the forum.

Manning’s commitment to our readers is to provide a venue where a meaningful dialog between individual readers and between readers and the authors can take place. It’s not a commitment to any specific amount of participation on the part of the authors, whose contribution to the forum remains voluntary (and unpaid). We suggest you try asking the authors some challenging questions lest their interest stray!

The Author Online forum and the archives of previous discussions will be accessible from the publisher’s website as long as the book is in print.

About the authors

PETER HILTON is a senior solution architect and operations director at Lunatech Research in Rotterdam, the Netherlands. Peter has focused on web application design and development since 1998, working mostly on Java web frameworks and web-based collaboration. In recent years, Peter has also applied agile software development processes and practices to technical project management. Since 2010, Peter has been a committer on the Play framework open source project and has presented Play at various European developer conferences. Together with colleagues at Lunatech, Peter is currently using Play to build web applications and web services for enterprise customers in the Netherlands and France. He’s on Twitter as @PeterHilton.

ERIK BAKKER has been building web applications since 2002 and is currently also employed by Lunatech Research. He put his first Scala application in production in early 2010 and has worked with Play 2 since its inception. Erik is a Play module contributor and has presented and blogged about the Play framework and Scala. You can find him on Twitter as @eamelink.

FRANCISCO JOSÉ CANEDO DOMINGUEZ joined Lunatech Research as a software developer in 2005. He started his professional career in 1997 and has comfortably worked with languages as diverse as C, C++, Java, XSLT, JavaScript, HTML, and Bash. He’s been exploring the power of Scala since 2010. Having had first-hand experience with several different web frameworks, Francisco finds Play’s approach to be a breath of fresh air. He is @fcanedo on Twitter.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset