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!
Features
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
Maven
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.
Artifacts
After importing the BOM in your pom.xml you can easily declare the modules.
Download
You can download the latest version directly from GitHub:
You can build the latest version directly from source. Just run:
Configuration
flash-messages is configured using springJavaConfig. 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).
Custom configuration
To modify the default behavior of flash-messages just extend FlashMessagesConfigurerAdapter and override those methods that you want to customize.
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.
Painting messages
Finally, you must to include the <flash:messages /> taglib in your views (or better in your decorator template).