Revisiting Giflib4:55 with Chris Ramacciotti
This course builds on the animated GIF library coded in Spring Basics. In this video, we cover the codebase that will serve as our starting point.
Git Command to Sync Your Code to the Start of this Video
git checkout -f s1v3
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.
In this course we'll be adding database connectivity to a static 0:00 animated gif library called giflib. 0:03 You may be familiar with this project from an earlier course. 0:06 If you have this code and want to use it, 0:09 just know that our starting point here is a bit different from where we left off. 0:11 I've created the GitHub repo that will be our starting point. 0:16 We'll start by highlighting the existing organization of the application so 0:19 you'll be able to see the differences. 0:23 Let's head to IntelliJ now. 0:25 In IntelliJ, you'll want to import the project I've made available to you, 0:28 either by downloading the zip file or by cloning the GitHub repository. 0:32 Check the teachers notes for info on the GitHub repository option. 0:36 After importing you should have the same project structure that you see here. 0:40 Let's take a few minutes to talk about what's in this project. 0:45 First this is a gradle project which means you'll find a build.gradle file here, 0:49 which is the one that I have open. 0:54 In this build file you'll see a line for 0:57 the spring-boot-gradle-plugin, as well as dependencies down the line here for 0:59 the spring-boot-starter-thymeleaf library and the hashids library. 1:04 This library allows us to easily generate unique hashes for our gifs. 1:09 These hashes are unique identifiers that are separate from the ids we'll assign to 1:13 each gif. 1:17 You've probably seen this floating around on the Internet with URL 1:18 shortening services like t.co for Twitter. 1:21 In a moment I'll show you how to configure the hashid functionality so 1:24 that it's available for autowiring into components like controllers. 1:27 Next we have our typical directory structure that aligns with the Java plugin 1:31 of Gradle, with source/main/java as the root for our application source code. 1:35 And source main resources being the place where we put anything that needs to be on 1:41 the classpath as well as a place to put static assets for 1:46 our web app located in the static directory. 1:49 And Thymeleaf templates are in here as well. 1:53 If I expand templates, I'll see a couple directories here. 1:55 I've just organized them by entity. 1:59 I see a details, form, and index page for 2:01 each one of the entities we'll be describing, as well as a favorites page 2:04 down here that will render a list of favorite gifs in our application. 2:09 One note about this static directory, 2:15 in spring basics we put our gif image files here. 2:18 Since we now want to add the ability for users to maintain this data by uploading 2:21 or removing, these gif images are no longer static data. 2:26 So instead will store the image data in the database. 2:30 Also when the resources directory is our hibernate configuration file 2:34 which you remember from hibernate basics. 2:38 This is an XML format. 2:41 Now I’ll collapse the resources directory and let's focus now on the main 2:44 package of our application source code and that's com.teamtreehouse.giflib. 2:48 If I expand that I'll see three more packages config, model, and web. 2:52 This config packages where I’ve put all source code that represents configuration 2:58 for our spring app. 3:02 In spring basics we stuck our configuration in what here I've called 3:04 Application.java but ultimately I want my Java classes to serve a single purpose. 3:08 You could consider this an example of what's called the single responsibility 3:15 principle which we'll talk about later in the course. 3:18 So, I have all my configuration for 3:21 the Thymeleaf templateEngine in its own config file called template config. 3:24 Just to demonstrate what it looks like to configure a template engine in spring. 3:29 AppConfig contains application level configuration. 3:33 And in this case consists only of a bean that initializes our hashid functionality. 3:37 Now initializing this hashid functionality, 3:44 I construct this hashids object and pass it what's called a salt, 3:46 which is kind of like initializing a random number generator with the seed. 3:52 I'll talk later about where this value comes from, 3:56 it comes from a properties file just as a hint of what's to come. 4:00 Moving on, there is the model package, which contains the classes for 4:04 our two entities in this application. 4:07 One for category and one for gif. 4:10 As a final note, categories in this app have colors associated with them, 4:13 which is why I have included this color enum here. 4:18 You can see, I have some predefined colors, aqua, blue, purple, fuchsia, 4:21 orange and gold with their associated hexadecimal codes. 4:26 That will be used in combination with our Thymeleaf templates. 4:31 As well as our CSS to style them on the front end. 4:36 Not that you've got your project started and have a grasp of how things 4:42 are organized, we're ready to move on to hibernate configuration in spring. 4:45 Just to brush up on your spring knowledge, 4:50 why don't you answer a few questions first. 4:52
You need to sign up for Treehouse in order to download course files.Sign up