alvinalexander.com | career | drupal | java | mac | mysql | perl | scala | uml | unix  

Play Framework/Scala example source code file (Controller.java)

This example Play Framework source code file (Controller.java) is included in my "Source Code Warehouse" project. The intent of this project is to help you more easily find Play Framework (and Scala) source code examples by using tags.

All credit for the original source code belongs to Play Framework; I'm just trying to make examples easier to find. (For my Scala work, see my Scala examples and tutorials.)

Play Framework tags/keywords

context, controller, flash, headernames, i18n, lang, mvc, play, play framework, request, response, session, status, string

The Controller.java Play Framework example source code

/*
 * Copyright (C) 2009-2013 Typesafe Inc. <http://www.typesafe.com>
 */
package play.mvc;

import play.i18n.Lang;
import play.mvc.Http.*;


/**
 * Superclass for a Java-based controller.
 */
public abstract class Controller extends Results implements Status, HeaderNames {

    /**
     * Returns the current HTTP context.
     */
    public static Context ctx() {
        return Http.Context.current();
    }

    /**
     * Returns the current HTTP request.
     */
    public static Request request() {
        return Http.Context.current().request();
    }

    /**
     * Returns the current lang.
     */
    public static Lang lang() {
        return Http.Context.current().lang();
    }

    /**
     * Change durably the lang for the current user
     * @param code New lang code to use (e.g. "fr", "en-US", etc.)
     * @return true if the requested lang was supported by the application, otherwise false.
     */
    public static boolean changeLang(String code) {
        return Http.Context.current().changeLang(code);
    }

    /**
     * Change durably the lang for the current user
     * @param lang New Lang object to use
     * @return true if the requested lang was supported by the application, otherwise false.
     */
    public static boolean changeLang(Lang lang) {
        return Http.Context.current().changeLang(lang);
    }

    /**
     * Clear the lang for the current user.
     */
    public static void clearLang() {
        Http.Context.current().clearLang();
    }

    /**
     * Returns the current HTTP response.
     */
    public static Response response() {
        return Http.Context.current().response();
    }

    /**
     * Returns the current HTTP session.
     */
    public static Session session() {
        return Http.Context.current().session();
    }

    /**
     * Puts a new value into the current session.
     */
    public static void session(String key, String value) {
        session().put(key, value);
    }

    /**
     * Returns a value from the session.
     */
    public static String session(String key) {
        return session().get(key);
    }

    /**
     * Returns the current HTTP flash scope.
     */
    public static Flash flash() {
        return Http.Context.current().flash();
    }

    /**
     * Puts a new value into the flash scope.
     */
    public static void flash(String key, String value) {
        flash().put(key, value);
    }

    /**
     * Returns a value from the flash scope.
     */
    public static String flash(String key) {
        return flash().get(key);
    }

}

Other Play Framework source code examples

Here is a short list of links related to this Play Framework Controller.java source code file:

... this post is sponsored by my books ...

#1 New Release!

FP Best Seller

 

new blog posts

 

Copyright 1998-2021 Alvin Alexander, alvinalexander.com
All Rights Reserved.

A percentage of advertising revenue from
pages under the /java/jwarehouse URI on this website is
paid back to open source projects.