§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. If you want to personalize package (or class name) you must modify application.global
key into conf/application.conf
.
import play.*;
public class Global extends GlobalSettings {
}
§Intercepting application start-up and shutdown
You can override the onStart
and onStop
operation to be notified of the corresponding application lifecycle events:
import play.*;
public class Global extends GlobalSettings {
public void onStart(Application app) {
Logger.info("Application has started");
}
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. You can override this:
import play.*;
import play.mvc.*;
import play.mvc.Http.*;
import play.libs.F.*;
import static play.mvc.Results.*;
public class Global extends GlobalSettings {
public Promise<SimpleResult> onError(RequestHeader request, Throwable t) {
return Promise.<SimpleResult>pure(internalServerError(
views.html.errorPage.render(t)
));
}
}
§Handling action not found
If the framework doesn’t find an action method for a request, the onHandlerNotFound
operation will be called:
import play.*;
import play.mvc.*;
import play.mvc.Http.*;
import play.libs.F.*;
import static play.mvc.Results.*;
public class Global extends GlobalSettings {
public Promise<SimpleResult> onHandlerNotFound(RequestHeader request) {
return Promise.<SimpleResult>pure(notFound(
views.html.notFoundPage.render(request.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 play.mvc.Http.*;
import play.libs.F.*;
import static play.mvc.Results.*;
public class Global extends GlobalSettings {
public Promise<SimpleResult> onBadRequest(RequestHeader request, String error) {
return Promise.<SimpleResult>pure(badRequest("Don't try to hack the URI!"));
}
}
Next: Intercepting requests
Found an error in this documentation? The source code for this page can be found here. After reading the documentation guidelines, please feel free to contribute a pull request. Have questions or advice to share? Go to our community forums to start a conversation with the community.