Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions grails-doc/src/en/guide/reference.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -1138,6 +1138,11 @@ include::ref/Tags/fieldValue.adoc[]

include::ref/Tags/findAll.adoc[]

[[ref-tags-flashMessages]]
==== flashMessages

include::ref/Tags/flashMessages.adoc[]

[[ref-tags-form]]
==== form

Expand Down
23 changes: 22 additions & 1 deletion grails-doc/src/en/guide/upgrading/upgrading71x.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -721,4 +721,25 @@ If this causes issues with existing URL mappings, you can disable it in `applica
grails:
urlmapping:
validateWildcards: false
----
----

===== 2.10 Flash Messages Tag (`g:flashMessages`)

Grails 7.1 introduces a new `<g:flashMessages />` tag that renders `flash.message`, `flash.error`, and `flash.warning` as Bootstrap 5 dismissible alerts with appropriate styling (success, danger, and warning respectively).

The tag automatically prevents duplicate rendering -- if called in both a page and its layout, only the first invocation produces output. All flash content is HTML-encoded to prevent XSS.

The default layout (`main.gsp`) and all scaffolding templates now use this tag. If your application has custom views that render flash messages manually, you can replace the boilerplate:

[source,xml]
----
<%-- Before --%>
<g:if test="${flash.message}">
<div class="alert alert-primary" role="alert">${flash.message}</div>
</g:if>

<%-- After --%>
<g:flashMessages />
----

The tag supports optional attributes for customizing CSS classes, icons, ARIA role, and dismissibility. See the {gspTagsRef}flashMessages.html[flashMessages] tag reference for full details.
120 changes: 120 additions & 0 deletions grails-doc/src/en/ref/Tags - GSP/flashMessages.adoc
Original file line number Diff line number Diff line change
@@ -0,0 +1,120 @@
////
Licensed to the Apache Software Foundation (ASF) under one
or more contributor license agreements. See the NOTICE file
distributed with this work for additional information
regarding copyright ownership. The ASF licenses this file
to you under the Apache License, Version 2.0 (the
"License"); you may not use this file except in compliance
with the License. You may obtain a copy of the License at

https://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing,
software distributed under the License is distributed on an
"AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
KIND, either express or implied. See the License for the
specific language governing permissions and limitations
under the License.
////


== 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:

[source,xml]
----
<g:flashMessages />
----

In a layout (safe to use alongside page-level usage -- the tag skips rendering if already called):

[source,xml]
----
<g:flashMessages />
<g:layoutBody/>
----

Non-dismissible alerts:

[source,xml]
----
<g:flashMessages dismissible="false" />
----

Custom ARIA role:

[source,xml]
----
<g:flashMessages role="status" />
----

Custom styling for success messages:

[source,xml]
----
<g:flashMessages messageClass="alert alert-info" messageIcon="bi bi-info-circle me-2" />
----

Setting flash messages in a controller:

[source,groovy]
----
// 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:

[cols="1,2,2"]
|===
| Flash Key | Default Alert Class | Default Icon

| `flash.message`
| `alert alert-success alert-dismissible fade show`
| `bi bi-check-circle me-2`

| `flash.error`
| `alert alert-danger alert-dismissible fade show`
| `bi bi-exclamation-triangle me-2`

| `flash.warning`
| `alert alert-warning alert-dismissible fade show`
| `bi bi-exclamation-circle me-2`
|===

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.

Attributes

* `messageClass` (optional) - CSS class for `flash.message` alerts. Default: `alert alert-success alert-dismissible fade show`
* `messageIcon` (optional) - Icon class for `flash.message` alerts. Default: `bi bi-check-circle me-2`
* `errorClass` (optional) - CSS class for `flash.error` alerts. Default: `alert alert-danger alert-dismissible fade show`
* `errorIcon` (optional) - Icon class for `flash.error` alerts. Default: `bi bi-exclamation-triangle me-2`
* `warningClass` (optional) - CSS class for `flash.warning` alerts. Default: `alert alert-warning alert-dismissible fade show`
* `warningIcon` (optional) - Icon class for `flash.warning` alerts. 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`
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@

<div class="bg-body-tertiary">
<div class="container-lg py-4">
<g:flashMessages />
<g:layoutBody/>
</div>
</div>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -471,4 +471,68 @@ class ApplicationTagLib implements ApplicationContextAware, InitializingBean, Gr
// encoding is handled in GroovyPage.invokeTag and GroovyPage.captureTagOutput
body()
}

/**
* Renders flash.message, flash.error, and flash.warning as Bootstrap alert divs.
* Automatically skips rendering if already called during this request, preventing
* duplicate display when used in both pages and layouts.
*
* @emptyTag
*
* @attr messageClass CSS class for flash.message alerts (default: 'alert alert-success alert-dismissible fade show')
* @attr messageIcon Icon class for flash.message alerts (default: 'bi bi-check-circle me-2')
* @attr errorClass CSS class for flash.error alerts (default: 'alert alert-danger alert-dismissible fade show')
* @attr errorIcon Icon class for flash.error alerts (default: 'bi bi-exclamation-triangle me-2')
* @attr warningClass CSS class for flash.warning alerts (default: 'alert alert-warning alert-dismissible fade show')
* @attr warningIcon Icon class for flash.warning alerts (default: 'bi bi-exclamation-circle me-2')
* @attr role ARIA role for alert divs (default: 'alert')
* @attr dismissible Whether to show a close button (default: true)
*/
Closure flashMessages = { attrs ->
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We discussed this in the weekly and we need to release 7.1 soon. For a quick resolution here and to make it in 7.1, we should either make this configurable at the app level or move it to the scaffolding project with a custom tag lib. Otherwise, we can revisit this in 8.0.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@jdaugherty I don't think it is really has anything to do with scaffolding, so I can introduce config values with bootstrap defaults.

if (request.getAttribute('_flashRendered')) {
return
}

boolean rendered = false
boolean dismissible = attrs.dismissible != null ? attrs.dismissible.toString().toBoolean() : true
String role = attrs.role ?: 'alert'

if (flash.message) {
renderFlashAlert(
attrs.messageClass ?: 'alert alert-success alert-dismissible fade show',
attrs.messageIcon ?: 'bi bi-check-circle me-2',
flash.message, dismissible, role)
rendered = true
}

if (flash.error) {
renderFlashAlert(
attrs.errorClass ?: 'alert alert-danger alert-dismissible fade show',
attrs.errorIcon ?: 'bi bi-exclamation-triangle me-2',
flash.error, dismissible, role)
rendered = true
}

if (flash.warning) {
renderFlashAlert(
attrs.warningClass ?: 'alert alert-warning alert-dismissible fade show',
attrs.warningIcon ?: 'bi bi-exclamation-circle me-2',
flash.warning, dismissible, role)
rendered = true
}

if (rendered) {
request.setAttribute('_flashRendered', true)
}
}

private void renderFlashAlert(String cssClass, String icon, Object message, boolean dismissible, String role) {
out << "<div class=\"${cssClass}\" role=\"${role}\">"
out << "<i class=\"${icon}\"></i>"
out << message.toString().encodeAsHTML()
if (dismissible) {
out << '<button type="button" class="btn-close" data-bs-dismiss="alert" aria-label="Close"></button>'
}
out << '</div>'
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@

<div class="bg-body-tertiary">
<div class="container-lg py-4">
<g:flashMessages />
<g:layoutBody/>
</div>
</div>
Expand Down
4 changes: 1 addition & 3 deletions grails-scaffolding/src/main/templates/scaffolding/create.gsp
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,7 @@
<section class="row">
<div id="create-${propertyName}" class="col-12 content scaffold-create" role="main">
<h1><g:message code="default.create.label" args="[entityName]" /></h1>
<g:if test="\${flash.message}">
<div class="message" role="status">\${flash.message}</div>
</g:if>
<g:flashMessages />
<g:hasErrors bean="\${this.${propertyName}}">
<ul class="alert alert-danger list-unstyled" role="alert">
<g:eachError bean="\${this.${propertyName}}" var="error">
Expand Down
4 changes: 1 addition & 3 deletions grails-scaffolding/src/main/templates/scaffolding/edit.gsp
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,7 @@
<section class="row">
<div id="edit-${propertyName}" class="col-12 content scaffold-edit" role="main">
<h1><g:message code="default.edit.label" args="[entityName]" /></h1>
<g:if test="\${flash.message}">
<div class="message" role="status">\${flash.message}</div>
</g:if>
<g:flashMessages />
<g:hasErrors bean="\${this.${propertyName}}">
<ul class="alert alert-danger list-unstyled" role="alert">
<g:eachError bean="\${this.${propertyName}}" var="error">
Expand Down
4 changes: 1 addition & 3 deletions grails-scaffolding/src/main/templates/scaffolding/index.gsp
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,7 @@
<div id="list-\${propertyName}" class="col-12 content scaffold-list" role="main">
<h1>
<g:message code="default.list.label" args="[entityName]" /></h1>
<g:if test="\${flash.message}">
<div class="alert alert-primary" role="alert"><i class="bi-info-circle"></i> \${flash.message}</div>
</g:if>
<g:flashMessages />
<f:table class="scaffold table table-striped table-sm" controller="\${controllerName}" collection="\${${propertyName}List}"/>

<g:if test="\${${propertyName}Count > params.int('max')}">
Expand Down
4 changes: 1 addition & 3 deletions grails-scaffolding/src/main/templates/scaffolding/show.gsp
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,7 @@
<section class="row">
<div id="show-${propertyName}" class="col-12 content scaffold-show" role="main">
<h1><g:message code="default.show.label" args="[entityName]" /></h1>
<g:if test="\${flash.message}">
<div class="alert alert-primary" role="alert"><i class="bi-info-circle"></i> \${flash.message}</div>
</g:if>
<g:flashMessages />
<f:display bean="${propertyName}" listClass="container" listItemClass="row mb-3" labelClass="form-label col-sm-3 text-sm-end" valueClass="col-sm-9" />
<g:form resource="\${this.${propertyName}}" controller="\${controllerName}" method="DELETE">
<fieldset class="bg-body-tertiary">
Expand Down
Loading
Loading