Flash messages

An easy way to send & show flash messages


Download Build Status Coverage Status Coverity Scan Build Status Project Stats

When applying the Post/Redirect/Get pattern in web application development, I run always into the same problem: how to communicate the result to the user after the redirection.

While it is a known problem and it has been resolved in other platforms (like Rails), Java does not seem to provide a simple and elegant solution.

flash-messages is an easy way to communicate flash messages after a redirection in Java web applications.

Today, you can use flash-messages in applications which use spring-mvc as web framework and Jstl to render views.

In future releases, it will be possible to use it in JavaEE applications and possibly with another view technologies like Thymeleaf or Freemarker.

Let’s start!


  • Seamless integration with @RequestMapping and @ExceptionHandler methods in the spring-mvc framework @Controller’s
  • Different levels of messages (ie. SUCCESS, INFO, WARNING, ERROR)
  • Resolution of i18n messages with arguments
  • Resolution of i18n arguments (ie Text, Link)
  • Easy integration with the Twitter Bootstrap alerts

Getting started

Get it into your project


Bill Of Materials (BOM)

flash-messages artifacts are in Maven Central and includes a BOM (Bill Of Materials) to facilitate the use of its modules.


After importing the BOM in your pom.xml you can easily declare the modules.

    <!-- ... -->
    <!-- ... -->


You can download the latest version directly from GitHub:

  • flash-messages-core (0.2.4)
  • flash-messages-spring (0.2.4)
  • flash-messages-taglibs (0.2.4)

Building from sources

You can build the latest version directly from source. Just run:

$ git clone https://github.com/jeslopalo/flash-messages.git
$ cd flash-messages
$ mvn clean package


flash-messages is configured using spring JavaConfig. It has been tested with versions greater or equal than 3.2.6.RELEASE.

Default configuration

In order to obtain the default configuration, just add @EnableFlashMessages in a @Configuration class (the same with @EnableWebMvc should be enough).

import es.sandbox.ui.messages.spring.config.annotation.EnableFlashMessages;
public class WebMvcConfigurer {
    public MessageSource messageSource() {      
        ReloadableResourceBundleMessageSource messageSource= new ReloadableResourceBundleMessageSource();
        return messageSource;

Custom configuration

To modify the default behavior of flash-messages just extend FlashMessagesConfigurerAdapter and override those methods that you want to customize.

import es.sandbox.ui.messages.Level;
import es.sandbox.ui.messages.CssClassesByLevel;
import es.sandbox.ui.messages.spring.config.annotation.EnableFlashMessages;
import es.sandbox.ui.messages.spring.config.annotation.FlashMessagesConfigurerAdapter;

public class CustomFlashMessagesConfigurer extends FlashMessagesConfigurerAdapter {

     * Sets the styles of flash-messages to be compatible
     * with twitter bootstrap alerts
     public void configureCssClassesByLevel(CssClassesByLevel cssClasses) {
        cssClasses.put(Level.ERROR, "alert alert-danger");

The main elements that can be configured or customized are: levels of messages, the css classes applied to the levels, the strategy to resolve i18n messages or modify the scope where messages are stored.

Writing messages

In order to write messages, just declare an argument of type Flash in the handler method (or in a @ExceptionHandler method), then you can add messages to the different levels.

@RequestMapping(value="/target", method= RequestMethod.POST)
String post(Flash flash, @ModelAttribute FormBackingBean form, BindingResult bindingResult) {
    if (bindingResult.hasErrors()) {        
        return "form";
    Result result= this.service.doSomething(form.getValue());
    if (result.isSuccessful()) {
        flash.success("messages.success-after-post", result.getValue());
        return "redirect:/successful-target-after-post";
    flash.error("messages.error-in-service", form.getValue());
    return "redirect:/error-target-after-post";

String handle(ServiceException exception, Flash flash) {
    return "somewhere";

Painting messages

Finally, you must to include the <flash:messages /> taglib in your views (or better in your decorator template).

<%@ taglib prefix="flash" uri="http://sandbox.es/tags/flash-messages" %>

<!-- ... -->
<flash:messages />
<!-- ... -->