<g:flashMessages />
flashMessages
Purpose
Renders flash.message, flash.error, and flash.warning as dismissible Bootstrap alert divs with appropriate styling. Automatically prevents duplicate rendering when used in both pages and layouts.
Examples
Basic usage in a view:
In a layout (safe to use alongside page-level usage — the tag skips rendering if already called):
<g:flashMessages />
<g:layoutBody/>
Non-dismissible alerts:
<g:flashMessages dismissible="false" />
Custom ARIA role:
<g:flashMessages role="status" />
Custom styling for success messages:
<g:flashMessages messageClass="alert alert-info" messageIcon="bi bi-info-circle me-2" />
Setting flash messages in a controller:
// Success message (renders as green alert)
flash.message = "Book '${book.title}' saved successfully."
// Error message (renders as red alert)
flash.error = "Unable to delete the record."
// Warning message (renders as yellow alert)
flash.warning = "You have unsaved changes."
Description
The flashMessages tag renders any combination of flash.message, flash.error, and flash.warning as Bootstrap 5 alert divs. Each flash key maps to a different alert style:
| Flash Key | Default Alert Class | Default Icon |
|---|---|---|
|
|
|
|
|
|
|
|
|
The tag automatically sets a _flashRendered request attribute after rendering. On subsequent calls within the same request, the tag detects this attribute and outputs nothing. This allows both pages and layouts to include <g:flashMessages /> without producing duplicate alerts — whichever renders first wins.
All flash message content is HTML-encoded to prevent XSS.
Configuration
The defaults target Bootstrap 5. Applications that use a different CSS framework can override the defaults globally in application.yml:
grails:
views:
gsp:
flashMessages:
messageClass: 'notification is-success'
messageIcon: 'fa-solid fa-check'
errorClass: 'notification is-danger'
errorIcon: 'fa-solid fa-triangle-exclamation'
warningClass: 'notification is-warning'
warningIcon: 'fa-solid fa-circle-exclamation'
role: 'alert'
dismissible: true
Resolution order for every default is:
-
Tag attribute (e.g.
<g:flashMessages messageClass="…" />) -
Configured value under
grails.views.gsp.flashMessages.* -
Built-in Bootstrap fallback
Attributes
-
messageClass(optional) - CSS class forflash.messagealerts. Default:alert alert-success alert-dismissible fade show -
messageIcon(optional) - Icon class forflash.messagealerts. Default:bi bi-check-circle me-2 -
errorClass(optional) - CSS class forflash.erroralerts. Default:alert alert-danger alert-dismissible fade show -
errorIcon(optional) - Icon class forflash.erroralerts. Default:bi bi-exclamation-triangle me-2 -
warningClass(optional) - CSS class forflash.warningalerts. Default:alert alert-warning alert-dismissible fade show -
warningIcon(optional) - Icon class forflash.warningalerts. Default:bi bi-exclamation-circle me-2 -
role(optional) - ARIA role for alert divs. Default:alert -
dismissible(optional) - Whether to show a close button. Default:true