Encapsulating a Flash Message3:43 with Chris Ramacciotti
After a brief discussion of the purpose of flash messages, this video demonstrates the code we'll need to store all data related to a message we want to display to a user, including the message text and status.
Git Command to Sync Your Code to the Start of this Video
git checkout -f s4v6
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.
During the second and 0:00 final part of user messaging, we begin to implement what's called a flash message. 0:01 As I mentioned earlier, these are messages that we display to users 0:06 indicating the status of the actions that they've taken. 0:10 For example, your GIF was successfully uploaded. 0:13 This style of messaging forms a key component in the feedback 0:16 loop of an application. 0:20 To illustrate what I mean, 0:21 think of an application as a conversation between coded software and its users. 0:23 As with any dialogue, there are two speakers. 0:28 The user speaks by navigating through the app and 0:30 performing any allowed actions within the app. 0:33 In our case, this could be adding a new category, uploading a new GIF or 0:37 editing and deleting categories and GIFs. 0:41 Or, it might just mean, browsing the library of GIFs. 0:44 This is how the user speaks to the app. 0:47 And how does the app speak back or respond? 0:50 By performing the actions requested by the user, either by displaying the items 0:53 requested or by taking action on the category or GIF. 0:57 In the case of adding a category for 1:01 example, a way the app can provide full communication to the user is by 1:03 informing the user of whether or not the action taken was completed successfully. 1:07 Without this key piece of information, we force the user into a state of confusion 1:12 as to whether or not something worked. 1:16 And we don't want confused users, we want informed, 1:19 happy users that know when something succeeded and when something failed. 1:22 This is accomplished with flash messaging. 1:27 We'll return to that keyword flash in the next video. 1:30 For now, let's add the necessary code that will capture all the appropriate 1:32 information related to a message that we want to display for the user. 1:36 For this app, we'll write a class called flash message. 1:40 That will contain a string message to display, 1:43 as well as a status level of success failure or info. 1:46 So that we'll be able to display and style different types of messages. 1:49 Let's get started. 1:53 So that we can encapsulate data related to a flash message into a single object. 1:55 Let's create a flash message class. 1:59 I'll place this in the web package, but outside of the controller package. 2:02 So right click, new java class, I'll call it FlashMessage. 2:06 Really, all we need for this class is the text of the message and 2:13 a status such as success or failure. 2:16 For the text, let's create a string field, 2:19 I'll call this message, private String message. 2:22 And for the status, how about we create a status enum that holds the two options 2:27 that I mention success and failure? 2:31 I'll get the field for that and they're now. 2:32 So private, we'll call this Status and the field name will be status as well. 2:35 And now let's create that enum. 2:41 So I'll do public static enum Status. 2:44 And the two options that I'd like to have available are SUCCESS and FAILURE. 2:50 When you're creating a Java enum, 2:54 the convention is to name your options with all caps. 2:57 Now, in order to be able to create one of these FlashMessage objects, 3:01 we'll need a constructor with both of these fields. 3:05 So I will do that now, I'll do that above that enum. 3:07 I'll use my IDE to generate those two. 3:11 I want a constructor that accepts one of each of those field values. 3:14 Great, and let's now add some getters so 3:19 that the Thymeleaf template will have access to these values. 3:22 Down below that, I will generate just getters for those, 3:25 I won't need setters, wouldn't hurt to throw me in here but I don't need them. 3:30 And there is our FlashMessage class. 3:34 Not too bad, right? 3:36 In the next video, 3:37 will leverage this class to display FlashMessages to our users. 3:38
You need to sign up for Treehouse in order to download course files.Sign up