Display temporary notifications after user actions. No external dependencies required.
@POST("/login")
public Object login(Request req, Response res) {
try {
if (Auth.login(req.queryParams("username"), req.queryParams("password"), req)) {
return redirectWithFlash(req, res, "success", "Welcome back!", "/dashboard");
}
return redirectWithFlash(req, res, "error", "Invalid credentials", "/login");
} catch (LoginLockedException e) {
return redirectWithFlash(req, res, "error", e.getMessage(), "/login");
}
}
redirectWithFlash(req, res, "success", "Operation completed!", "/");
redirectWithFlash(req, res, "error", "Something went wrong", "/");
redirectWithFlash(req, res, "info", "Check your email", "/");
redirectWithFlash(req, res, "warning", "Changes require restart", "/");
Add {{ flash() }} once in your base layout — it renders automatically when a flash message exists.
<body>
{% block content %}{% endblock %}
{{ flash() | raw }}
</body>
Use a @Config class to configure flash notifications at startup. See Config.
@Config
public class NotifConfig implements ConfigInterface {
@Override
public void configure() {
FlashConfig.setDuration(5000);
// Options: top-right, top-left, bottom-right,
// bottom-left, top-center, bottom-center
FlashConfig.setPosition("top-right");
FlashConfig.setCustomCSS("""
.flash-notification {
border-radius: 1rem;
font-size: 0.875rem;
}
.flash-success {
background: linear-gradient(to right, #10b981, #059669);
}
""");
}
}