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

Using custom actions


For this recipe, we will explore how Play Framework provides the building blocks for creating reusable, custom actions.

How to do it…

For Java, we need to take the following steps:

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

  2. Modify foo_java/app/controllers/Application.java by adding the following action:

        @With(AuthAction.class)
        public static Result dashboard() {
            return ok("User dashboard");
        }
    
        public static Result login() {
            return ok("Please login");
        }
  3. Add our new action class to foo_java/app/controllers/AuthAction.java as well:

    package controllers;
    
    import play.*;
    import play.mvc.*;
    import play.libs.*;
    import play.libs.F.*;
    
    public class AuthAction extends play.mvc.Action.Simple {
        public F.Promise<Result> call(Http.Context ctx) throws Throwable {
            Http.Cookie authCookie = ctx.request().cookie("auth");
    
            if (authCookie != null) {
              Logger.info("Cookie: " + authCookie);
              return delegate...