About this Book

This book will get you started developing web applications with Play 2, using the Java programming language. But, perhaps more importantly, it’ll also teach you the concepts behind web development with Play.

There are many Java web frameworks, but most of them have a key flaw: they try to hide the web behind an abstraction layer, rather than embracing it. That is the most important difference between Play and most other Java web frameworks (Servlets, we’re looking at you!).

Developing a web application with Play requires a certain mindset. Throughout the book, we try to teach you how to achieve that. If you’re a Java EE veteran, we’ll do our best to lessen the culture shock. But if you’re new to web development with Java, or web development in general, we’ve got your back, too. We do our best to explain everything about web development that you need to know.

The only assumption we make is that you have some background in Java programming—you should be comfortable reading and writing Java code. We’ve used Java 7 syntax throughout this book, since that is the supported version of Java at the time of writing.

You’ve probably already heard that Play 2 is written in Scala. That is absolutely true, and we feel that the language and the tools available for the platform are an excellent choice to write a web platform in. We also feel that there is no reason you should have to build a web application in Scala. Play developers apparently agree, because Play has a first-class Java API, which means you can write a full application using Play 2 without writing a single line of Scala.

In this book, we avoid discussing Scala wherever possible. There is one subject where this wasn’t avoidable: view templates in Play 2 are based on the Scala language. But in the rest of the book we treat it as just another template language, showing you the basic constructs you need to create your templates. We promise you don’t need to learn any Scala to follow along with this book.

As we write this, the current version of Play is 2.2. That means that all the code we demonstrate assumes a Play version of 2.2.x, and we’ve tested every code sample under version 2.2.0.

Roadmap

This book is organized in three parts. The first part introduces you to all the basic concepts of Play. The second part dives deeper into the core elements of a Play application, while the third part demonstrates more advanced things that you can do with Play. Here’s a quick overview of all the chapters.

Chapter 1 introduces Play, and highlights some of its important features. It then shows you how to install Play and create a simple “Hello World” application.

Chapter 2 takes a look at what makes up a Play application. It goes over all the directories and files, and explains what every component is for. We also show you how you can import a Play application into your IDE.

Chapter 3 shows a simple web application, without going into too much detail. In this chapter, we’ll see every important part of Play in action: controllers, actions, templates, and forms. This is the start of an application that we’ll develop in the book.

Chapter 4 takes a step back and looks at where Play fits in a more conventional enterprise architecture. It contrasts Play with conventional JEE development, and shows how Play can tackle major challenges in such an architecture.

Chapter 5 is all about controllers. It explains what controllers are and how action methods help you interface with the web. It also explains how routing works and introduces the different scopes.

Chapter 6 covers how to handle user input. We show you how to use Play’s Form API, as well as how binding and validation work.

Chapter 7 introduces database persistence. We explain the concept of an ORM and show you how to use the Ebean ORM tool with Play. At the end of the chapter, we discuss how you can use JPA with Play instead of Ebean, should you choose to do so.

Chapter 8 explains how view templates work. It explains the template syntax, and shows how to use composition to structure your pages in a reusable manner. At the end of the chapter, we look at using Play’s support for LESS and CoffeeScript, and introduce the internationalization API.

Chapter 9 covers one of the more powerful features of Play: asynchronous request handling. It explains why long-running tasks are better performed “in the background,” and how to achieve that easily. It also shows how you can have a web application with streaming data, using WebSockets or Comet.

Chapter 10 explains how you can build a secure application in Play. It also explains how you can avoid common security problems, and how you can use filters to implement authentication.

Chapter 11 covers the build process of Play. It explains the configuration files, and shows you how to package your code in reusable modules. Finally, it shows you what’s involved with taking your application to production.

Chapter 12 introduces the tools that Play has for testing your application. It explains the different kinds of automated tests there are, and how you can write them for your application.

Code conventions and downloads

All source code in listings or in text is in a fixed-width font like this to separate it from ordinary text. Code annotations accompany many of the listings, highlighting important concepts. The current version of Play is 2.2 at the time of writing. That means that all the code we demonstrate assumes a Play version of 2.2.x, and we’ve tested every code sample under version 2.2.0.

Source code for all working examples in this book is available for download from GitHub at https://github.com/playforjava, as well as from the publisher’s website at www.manning.com/PlayforJava.

Author Online

The purchase of Play for Java 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 other users. To access the forum and subscribe to it, visit www.manning.com/PlayforJava. 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 readers is to provide a venue for meaningful dialogue between individual readers and between readers and the authors. It is not a commitment to any specific amount of participation on the part of the authors, whose contribution to the forum remains voluntary (and unpaid). Let your voice be heard, and keep the authors on their toes!

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

Nicolas Leroux is a senior architect and Technical Director at Lunatech Research where he’s worked since 2001 and where he is mainly involved in JEE projects for Lunatech’s customers. Since 2009, Nicolas has developed a passion for the Play framework and is a core developer of its open source project. He was involved in the first version of the Play framework and is contributing to the second one. Nicolas also enjoys introducing Play to new audiences and is a frequent speaker at conferences.

SIETSE DE KAPER started his career as a software developer at Lunatech Research in 2007. He has worked on several commercial web applications using various web frameworks—mostly Java, but also PHP, Ruby, and Scala. Sietse started developing applications with the Play framework when version 1.0 came out in October 2009. After using Play on several projects, he now considers it the most effective framework in the Java ecosystem.

About the cover illustration

The figure on the cover of Play for Java is captioned a “Farmer from Dobrota, Montenegro.” The illustration is taken from the reproduction, published in 2006, of a nineteenth-century collection of costumes and ethnographic descriptions entitled Dalmatia by Professor Frane Carrara (1812–1854), an archaeologist and historian, and the first director of the Museum of Antiquity in Split, Croatia. The illustrations were obtained from a helpful librarian at the Ethnographic Museum (formerly the Museum of Antiquity), itself situated in the Roman core of the medieval center of Split: the ruins of Emperor Diocletian’s retirement palace from around AD 304. The book includes finely colored illustrations of figures from different regions of Dalmatia, accompanied by descriptions of the costumes and of everyday life.

Dobrota is a small town on the Adriatic coast, officially a part of the municipality of Kotor, an ancient Mediterranean port on Kotor Bay, surrounded by fortifications built in the Venetian period. Today it is increasingly a tourist destination, due to the dramatic limestone cliffs and beautiful coastal vistas. The man on the cover is wearing an embroidered vest over black woolen pantaloons and a wide colorful sash. He is carrying a long pipe, a musket, and has pistols inserted in his sash. The rich and colorful embroidery on his costume is typical for this region, and marks this as an outfit for special occasions and not for working the land.

Dress codes have changed since the nineteenth century, and the diversity by region, so rich at the time, has faded away. It is now hard to tell apart the inhabitants of different continents, let alone different towns or regions. Perhaps we have traded cultural diversity for a more varied personal life—certainly for a more varied and fast-paced technological life.

At a time when it is hard to tell one computer book from another, Manning celebrates the inventiveness and initiative of the computer business with book covers based on the rich diversity of regional life of two centuries ago, brought back to life by illustrations from collections such as this one.

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

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