4.13. Working with messages
Prev | 4. Rendering views | Next

## 4.13. Working with messages

Spring Web Flow's `MessageContext` is an API for recording messages during the course of flow executions. Plain text messages can be added to the context, as well as internationalized messages resolved by a Spring `MessageSource`. Messages are renderable by views and automatically survive flow execution redirects. Three distinct message severities are provided: `info`, `warning`, and `error`. In addition, a convenient `MessageBuilder` exists for fluently constructing messages.

### Adding plain text messages

    MessageContext context = ...
    MessageBuilder builder = new MessageBuilder();
    context.addMessage(builder.error().source("checkinDate")
        .defaultText("Check in date must be a future date").build());
    context.addMessage(builder.warn().source("smoking")
        .defaultText("Smoking is bad for your health").build());
    context.addMessage(builder.info()
        .defaultText("We have processed your reservation - thank you and enjoy your stay").build());

### Adding internationalized messages

    MessageContext context = ...
    MessageBuilder builder = new MessageBuilder();
    context.addMessage(builder.error().source("checkinDate").code("checkinDate.notFuture").build());
    context.addMessage(builder.warn().source("smoking").code("notHealthy")
        .resolvableArg("smoking").build());
    context.addMessage(builder.info().code("reservationConfirmation").build());

### Using message bundles

Internationalized messages are defined in message bundles accessed by a Spring `MessageSource`. To create a flow-specific message bundle, simply define `messages.properties` file(s) in your flow's directory. Create a default `messages.properties` file and a .properties file for each additional `Locale` you need to support.

    #messages.properties
    checkinDate=Check in date must be a future date
    notHealthy={0} is bad for your health
    reservationConfirmation=We have processed your reservation - thank you and enjoy your stay

From within a view or a flow, you may also access message resources using the `resourceBundle` EL variable:

    <h:outputText value="#{resourceBundle.reservationConfirmation}" />

### Understanding system generated messages

There are several places where Web Flow itself will generate messages to display to the user. One important place this occurs is during view-to-model data binding. When a binding error occurs, such as a type conversion error, Web Flow will map that error to a message retrieved from your resource bundle automatically. To lookup the message to display, Web Flow tries resource keys that contain the binding error code and target property name.

As an example, consider a binding to a `checkinDate` property of a `Booking` object. Suppose the user typed in a alphabetic string. In this case, a type conversion error will be raised. Web Flow will map the 'typeMismatch' error code to a message by first querying your resource bundle for a message with the following key:

    booking.checkinDate.typeMismatch

The first part of the key is the model class's short name. The second part of the key is the property name. The third part is the error code. This allows for the lookup of a unique message to display to the user when a binding fails on a model property. Such a message might say:

    booking.checkinDate.typeMismatch=The check in date must be in the format yyyy-mm-dd.

If no such resource key can be found of that form, a more generic key will be tried. This key is simply the error code. The field name of the property is provided as a message argument.

    typeMismatch=The {0} field is of the wrong type.

Prev | Up | Next
4.12. Executing view transitions | Home | 4.14. Displaying popups