public class FlashScope extends Object implements Jooby.Module
The flash scope is designed to transport success and error messages, between requests. The flash scope is similar to Session
but lifecycle is shorter: data are kept for only one request.
The flash scope is implemented as client side cookie, so it helps to keep application stateless.
{
use(new FlashScope());
get("/", req -> {
return req.ifFlash("success").orElse("Welcome!");
});
post("/", req -> {
req.flash("success", "The item has been created");
return Results.redirect("/");
});
}
FlashScope
is also available on mvc routes via Flash
annotation:
@Path("/")
public class Controller {
@GET
public Object flashScope(@Flash Map<String, String> flash) {
...
}
@GET
public Object flashAttr(@Flash String foo) {
...
}
@GET
public Object optionlFlashAttr(@Flash Optional<String> foo) {
...
}
}
Worth to mention that flash attributes are accessible from template engine by prefixing attributes with flash.
. Here is a handlebars.java
example:
{{#if flash.success}}
{{flash.success}}
{{else}}
Welcome!
{{/if}}
Constructor and Description |
---|
FlashScope()
Creates a new FlashScope .
|
FlashScope(Cookie.Definition cookie)
Creates a new FlashScope and customize the flash cookie.
|
Modifier and Type | Method and Description |
---|---|
void |
configure(Env env, com.typesafe.config.Config conf, com.google.inject.Binder binder)
Configure and produces bindings for the underlying application.
|
equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
config
public static final String NAME
public FlashScope(Cookie.Definition cookie)
FlashScope
and customize the flash cookie.
cookie
- Cookie template for flash scope.
public FlashScope()
FlashScope
.
public void configure(Env env, com.typesafe.config.Config conf, com.google.inject.Binder binder)
Jooby.Module
application env
and/or the current application properties available from Config
.
configure
in interface Jooby.Module
env
- The current application's env. Not null.
conf
- The current config object. Not null.
binder
- A guice binder. Not null.
Copyright © 2019. All rights reserved.