Bummer! This is just a preview. You need to be signed in with a Basic account to view the entire video.
Making Flash Messages Survive a Redirect3:48 with Chris Ramacciotti
Because flash messages will communicate a message that relevant only immediately following an action and a redirect, in this video we discuss how to make those flash messages survive a redirect. We'll employ the same kind of object as we did for form validation results, and that is a RedirectAttributes object.
Git Command to Sync Your Code to the Start of this Video
git checkout -f s4v7
Using Github With This Course
You can complete this course entirely using code created by you on your local machine. However, if you choose to use the code I've made available to you, you have two options:
- Use the project files linked at the bottom of this page, or
- Use the Github repository I've made available (recommended)
If you choose the recommended option of using the Github repository, it can be found at
To utilize Github with this course, you can download the Github desktop client for your system or use the command line interface provided with Git.
Clone this repository to your machine using the Github desktop client, or using the following command:
git clone firstname.lastname@example.org:treehouse/giflib-hibernate.git
To update your local repository to match a video's starting point, you can use the
git checkout command in combination with the stage and video number. For example, to update your local repository to match the starting point of Stage 5, Video 4, you'd use the following:
git checkout -f s5v4
Notice the use of the -f option. This forces Git to override all local changes, so be aware: this will cause any changes you made to be lost.
Now that we have a class that we can use to store flash messages, it's time to
jump to the CategoryController and see how we'll create those flash message objects
and make them available even after redirecting.
What you may not have realized is that our Thymeleaf templates are already configured
to display flash messages.
Let me show you.
If you open the layout.html,
you'll see this fragment that contains all the data for a flash message.
Now this fragment is only displayed if an attribute named flash is not null
in the model map, that is, if an attribute named flash is present and not null.
You'll see both the status here and
the class name that is converted to lowercase and
the text of that message from that flash object in this div that's displayed here.
So this is a Thymeleaf fragment that can be used for
display in any of the other templates.
So if you switch to the category form up at the top,
you see that we do indeed use that flash fragment.
So your Thymeleaf templates are already configured to display flash messages.
And you could probably figure out that what we need to do is make sure that we
add that flash attribute to the model map so that messages are displayed.
Let's go ahead and do that.
We're gonna do that in, you guessed it, the CategoryController.
Now, you've already seen the way to add attributes to the model such that they
survive a redirect, and this is exactly what we need to do for flash messages.
So before redirecting, let's add the flashMessage object.
We'll do that right here.
So we'll add that to the redirectAttributes object,
And inside parentheses what we want to do is add an attribute named flash.
That's what the template will be expecting.
And the value for that attribute will be a new FlashMessage object.
And here we have to specify both the message we want to display as well as that
So let's do that right now.
So this is when a new category was successfully saved.
So I will make my message say Category successfully added.
And then the status is going to be,
need to use FlashMessage.Status.SUCCESS.
Now if you didn't wanna use FlashMessage.Status.SUCCESS and
instead you wanted to use Status.SUCCESS,
what you could do is do a static import of FlashMessage.Status.
I'm gonna leave mine here for now.
Okay, with that in place, let's reboot our app and give this a whirl.
Looks like my app is still running, so I will kill that and let's reboot.
Looks like Spring is booting up.
And the application has started successfully.
So let's switch to Chrome.
And I'll now add a category with valid data and see if we get our flash message.
Let's add one named Validation.
And I will make that fuchsia, and click Add.
And great, there's our flash message.
And just to test the flash nature of this message,
I can see that on refresh, the message disappears.
This is exactly what we want.
Hey, congratulations on your implementation of validation and
You need to sign up for Treehouse in order to download course files.Sign up