Skip to content
Guillaume Bort edited this page Feb 6, 2012 · 5 revisions

Application global settings

The Global object

Defining a Global object in your project allows you to handle global settings for your application. This object must be defined in the root package.

import play.*;

public class Global extends GlobalSettings {

}

Hooking application start and stop

You can override the onStart and onStop operation to be notified of the events in the application lifecycle;

import play.*;

public class Global extends GlobalSettings {

  @Override
  public void onStart(Application app) {
    Logger.info("Application has started");
  }  
  
  @Override
  public void onStop(Application app) {
    Logger.info("Application shutdown...");
  }  
    
}

Providing an application error page

When an exception occurs in your application, the onError operation will be called. The default is to use the internal framework error page:

import play.*;
import play.mvc.*;

import static play.mvc.Results.*;

public class Global extends GlobalSettings {

  @Override
  public Result onError(Throwable t) {
    return internalServerError(
      views.html.errorPage(t)
    );
  }  
    
}

Handling action not found

If the framework doesn’t find an action method for a request, the onActionNotFound operation will be called:

import play.*;
import play.mvc.*;

import static play.mvc.Results.*;

public class Global extends GlobalSettings {

  @Override
  public Result onActionNotFound(String uri) {
    return notFound(
      views.html.pageNotFound(uri)
    );
  }  
    
}

The onBadRequest operation will be called if a route was found, but it was not possible to bind the request parameters:

import play.*;
import play.mvc.*;

import static play.mvc.Results.*;

public class Global extends GlobalSettings {

  @Override
  public Result onBadRequest(String uri, String error) {
    return badRequest("Don't try to hack the URI!");
  }  
    
}

Next: Testing your application

Clone this wiki locally