Book Image

Play Framework Cookbook - Second Edition

By : Alexander Reelsen, Giancarlo Inductivo
Book Image

Play Framework Cookbook - Second Edition

By: Alexander Reelsen, Giancarlo Inductivo

Overview of this book

<p>As web and mobile systems become more sophisticated, anchoring systems in a mature, solid framework has become increasingly important. Play 2 provides developers with the necessary tools to build robust web applications.</p> <p>This book is a compilation of useful recipes aimed at helping developers discover the power of Play 2. The introductory section serves as a primer to Play Framework, wherein all the fundamentals of the framework are covered extensively. It then explains the usage of controllers and how modules can be leveraged for optimal performance. Next, the book walks you through creating and using APIs, followed by extensive real-world applications. Finally, you will learn to manage applications post production.</p>
Table of Contents (15 chapters)
Play Framework Cookbook Second Edition
Credits
About the Authors
About the Reviewers
www.PacktPub.com
Preface
Index

Working with XML and text files


Using View templates, we are also able to respond to HTTP requests in other content types such as text files and XML data formats. Play Framework has native handlers for XML and text file content type responses.

How to do it...

For Java, we need to take the following steps:

  1. Run the foo_java application with Hot-Reloading enabled:

        $ activator "~run"
    
  2. Create the text-based view template file products.scala.txt in app/views/ and add the following content:

        @(productMap: Map[Integer, String])
        @for((id, name) <- productMap) {
          The Product '@name' has an ID of @id
        }
  3. Create the XML-based view template file products.scala.xml in app/views/ and add the following content:

        @(productMap: Map[Integer, String]) <products>
        @for((id, name) <- productMap) {
          <product id="@id">@name</product>
        }
        </products>
  4. Edit foo_java/app/controllers/Application.java by adding the following actions:

        public static Result listProductsAsXML() {
            return ok(views.xml.products.render(productMap));
        }
    
        public static Result listProductsAsTXT() {
            return ok(views.txt.products.render(productMap));
        }
  5. Edit foo_java/conf/routes by adding the following lines:

        GET    /products.txt    controllers.Application.listProductsAsTXT()
        GET    /products.xml    controllers.Application.listProductsAsXML()
  6. View the new routes and actions using a web browser:

    • http://localhost:9000/products.txt and,

    • http://localhost:9000/products.xml

For Scala, we need to take the following steps:

  1. Run the foo_scala application with Hot-Reloading enabled:

        $ activator "~run"
  2. Create the text-based view template file products.scala.txt in app/views/ and add the following content:

        @(productMap: Map[Int, String])
        @for((id, name) <- productMap) {
          The Product '@name' has an ID of @id
        }
  3. Create the XML-based view template file products.scala.xml in app/views/ and add the following content:

        @(productMap: Map[Int, String]) <products>
        @for((id, name) <- productMap) {
          <product id="@id">@name</product>
        }
        </products>
  4. Edit foo_scala/app/controllers/Application.scala by adding the following actions:

        def listProductsAsTXT = Action {
          Ok(views.txt.products(productMap))
        }
    
        def listProductsAsXML = Action {
          Ok(views.xml.products(productMap))
        }
  5. Edit foo_scala/conf/routes by adding the following lines:

        GET    /products.txt    controllers.Application.listProductsAsTXT
        GET    /products.xml    controllers.Application.listProductsAsXML
  6. View the new routes and actions using a web browser:

    • http://localhost:9000/products.txt and

    • http://localhost:9000/products.xml

How it works...

In this recipe, we utilized build-in support for other content types in Play Framework. We created new URL routes and web actions to be able to respond to requests for data in XML or text file formats. By following file naming standards and convention for views, we were able to create view templates in HTML, XML, and text file formats, which Play automatically handles, and then adds the appropriate content type headers in the HTTP response.