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 email@example.com: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 0:00 jump to the CategoryController and see how we'll create those flash message objects 0:03 and make them available even after redirecting. 0:08 What you may not have realized is that our Thymeleaf templates are already configured 0:12 to display flash messages. 0:16 Let me show you. 0:18 If you open the layout.html, 0:19 you'll see this fragment that contains all the data for a flash message. 0:22 Now this fragment is only displayed if an attribute named flash is not null 0:28 in the model map, that is, if an attribute named flash is present and not null. 0:35 You'll see both the status here and 0:40 the class name that is converted to lowercase and 0:41 the text of that message from that flash object in this div that's displayed here. 0:45 So this is a Thymeleaf fragment that can be used for 0:53 display in any of the other templates. 0:57 So if you switch to the category form up at the top, 1:00 you see that we do indeed use that flash fragment. 1:03 So your Thymeleaf templates are already configured to display flash messages. 1:09 And you could probably figure out that what we need to do is make sure that we 1:15 add that flash attribute to the model map so that messages are displayed. 1:19 Let's go ahead and do that. 1:23 We're gonna do that in, you guessed it, the CategoryController. 1:24 Now, you've already seen the way to add attributes to the model such that they 1:30 survive a redirect, and this is exactly what we need to do for flash messages. 1:34 So before redirecting, let's add the flashMessage object. 1:38 We'll do that right here. 1:43 So we'll add that to the redirectAttributes object, 1:45 addFlashAttributes. 1:50 And inside parentheses what we want to do is add an attribute named flash. 1:52 That's what the template will be expecting. 1:57 And the value for that attribute will be a new FlashMessage object. 1:58 And here we have to specify both the message we want to display as well as that 2:05 status. 2:10 So let's do that right now. 2:11 So this is when a new category was successfully saved. 2:13 So I will make my message say Category successfully added. 2:17 And then the status is going to be, 2:23 need to use FlashMessage.Status.SUCCESS. 2:26 Now if you didn't wanna use FlashMessage.Status.SUCCESS and 2:31 instead you wanted to use Status.SUCCESS, 2:37 what you could do is do a static import of FlashMessage.Status. 2:41 I'm gonna leave mine here for now. 2:47 Okay, with that in place, let's reboot our app and give this a whirl. 2:50 Looks like my app is still running, so I will kill that and let's reboot. 2:55 Looks like Spring is booting up. 3:05 And the application has started successfully. 3:08 So let's switch to Chrome. 3:12 And I'll now add a category with valid data and see if we get our flash message. 3:15 Let's add one named Validation. 3:21 And I will make that fuchsia, and click Add. 3:26 And great, there's our flash message. 3:30 And just to test the flash nature of this message, 3:33 I can see that on refresh, the message disappears. 3:36 This is exactly what we want. 3:41 Hey, congratulations on your implementation of validation and 3:43 flash messages. 3:46
You need to sign up for Treehouse in order to download course files.Sign up