Bummer! This is just a preview. You need to be signed in with a Basic account to view the entire video.
Start a free Basic trial
to watch this video
In this video, you are tasked with creating a controller to handle requests related to categories.
-
0:00
With our model and data repository in place, let's move on to the controller.
-
0:05
We'll need a controller for categories that can capture requests to a URI for
-
0:09
all the categories and a URI for a single category.
-
0:13
So, here's your next task.
-
0:16
Create a Category Controller with one method [SOUND] that captures requests to
-
0:20
/categories and returns a String.
-
0:23
This method should fetch the entire list of category objects from
-
0:26
an auto-wired category repository and add it to a ModelMap.
-
0:30
[SOUND] Then, the method should return the string categories to associate it
-
0:35
with the provided Thymeleaf Template.
-
0:37
Are you ready for this?
-
0:39
Okay.
-
0:40
Go ahead, have at it.
-
0:41
But wait, remember to use the GIF controller as an example to follow.
-
0:46
Ready, set, code.
-
0:48
>> All right, what you're looking at here is my category controller class,
-
0:52
which I put in the controller package with the other controller.
-
0:55
You can see that I've annotated the class with Controller and included an Autowired
-
1:00
category repository that I can use in the controller method down here.
-
1:05
As for this method, I've annotated it with request mapping to capture
-
1:09
any requests for the category list page, which is /categories.
-
1:14
Here is my modelMap parameter, and inside is the the call to the repository's
-
1:19
getALLCategories method that we wrote together earlier.
-
1:22
I added the list that was returned by this method to the modelMap using
-
1:27
modelmap.put here.
-
1:28
And here I've returned the name of the time leave template, or categories.
-
1:34
This is will ensure that it's the categories.html template that will be
-
1:37
rendered when this page is requested.
-
1:41
The last piece of this controller, we'll do together.
-
1:44
We'll need another method that will capture requests to an individual
-
1:46
category.
-
1:47
To start that, I'll request a mapping to a single category page,
-
1:52
category/, and here we're going to use an integer id.
-
1:57
The name won't work well as a URI placeholder
-
2:00
here because names could have spaces in them.
-
2:03
So instead, I'll use the integer id as a placeholder.
-
2:08
Then we'll start typing the method public String category.
-
2:14
And the first parameter I'm going to list is our @PathVariable.
-
2:18
That will capture the value that's passed into
-
2:21
the id placeholder that's in our RequestMapping annotation.
-
2:26
So I will call it id.
-
2:27
And of course, we'll include an instance of a modelMap that'll
-
2:31
be passed in by the Spring framework.
-
2:35
>> Now, I know I'm going to return the single categories template,
-
2:38
which is given in category.html, so I'll type in that return value right now.
-
2:44
That's going to be return "category".
-
2:49
Now I know the category page in the browser should probably
-
2:52
contain the category name, as well as
-
2:54
all GIFs related to that category, >> So by the time we reach the return
-
2:59
statement we better have added to the model map, both a category object
-
3:04
associated with this ID as well as a list of GIFs associated with this category.
-
3:11
Let's tackle each of those now.
-
3:13
First, the category object.
-
3:15
You just wrote a findById method, so
-
3:17
we'll call that now and add the result to the model map.
-
3:19
I'll create the category object,
-
3:27
And use the categoryRepository field and call its findById method
-
3:32
passing it the integer id that came into our path variable.
-
3:37
Then I'll add the category object that was returned by the findById method to
-
3:41
the model, using modelMap.put.
-
3:46
The name I'll give it will be simply category, and I'll pass category.
-
3:50
Whatever value was stored in there as a result of this return value,
-
3:54
I will add to the ModelMap.
-
3:57
Now onto the next item.
-
3:58
For a list of the GIFs associated with this category, we'll need to use a GIF
-
4:03
repository to fetch all those GIFs within a certain category ID, like this.
-
4:10
I'll give myself a couple lines to separate this code.
-
4:13
And the first thing I'll do is define a list that'll
-
4:15
hold that list of GIFs that comes back from the GIF repository.
-
4:19
Make sure to import the GIF class there, since we haven't done that yet.
-
4:24
And I'll call the gifRepositories,
-
4:28
findByCategoryId method, passing it the id.
-
4:33
And finally, adding that list to the model map with modelMap.put.
-
4:38
I'll call that "gifs" in the map key, and pass it the object gifs.
-
4:47
But wait, there are two problems with this.
-
4:50
First, there is no GIF repository variable defined.
-
4:53
And second, there is no findByCategoryId method defined for
-
4:57
a GIF repository object.
-
4:59
The first one we can fix right here.
-
5:00
We'll simply add an Autowired gift repository to the object.
-
5:06
So I'll add that with the @Autowired annotation, add a GifRepository.
-
5:14
That fixes the first error, but
-
5:16
we still have this error with the missing findByCategoryId method.
-
5:21
This one will take a bit more work.
-
5:23
But it's still pretty quick.
-
5:24
We'll hop over to the GifRepository class, and add a findByCategoryId method.
-
5:32
So let's scroll down here and I'll just add it to the bottom.
-
5:35
That'll return a list.
-
5:41
FindByCategoryId, and let's have it accept a single end parameter.
-
5:48
So this method, given some int id, will essentially iterate over our
-
5:53
list up here looking for objects that have a given category id.
-
5:58
If they haven't given a category id,
-
6:00
I will include those in the list that I return.
-
6:03
So to start, we better initialize a brand new empty list.
-
6:08
I'll make it a list of GIFs and initialize it as a new ArrayList.
-
6:18
Make sure to import those classes, excellent.
-
6:22
Now, let's use a for each loop to iterate over the list of ALL_GIFS.
-
6:28
So for every Gif, I keep typing Fig, how about Gif?
-
6:33
For every Gif in ALL_GIFS Let's
-
6:38
check to see if the category id of the Gif object in this iteration of the loop is
-
6:43
equal to the id value that was passed in as a parameter.
-
6:46
So, if gif.getCategoryId ==,
-
6:51
I can use double equals here.
-
6:54
It's a primitive value.
-
6:55
I don't need to use dot equals.
-
6:59
Then what I want to do is add that to the list.
-
7:05
So, by the time we get through this loop, any Gif in the list that has the category
-
7:10
ID that was passed in as a parameter value,
-
7:12
will be added to the list name Gifs.
-
7:14
And once I get through my loop, what I'd like to do is return
-
7:19
whatever list has resulted by iterating over all gifs.
-
7:24
Now that we've coded this method, after we jumped back to the category controller
-
7:28
class, we should see that that error right there has been resolved,
-
7:33
since we created that method.
-
7:36
At this point, we should at least be able to see the category list page,
-
7:40
even if it hasn't been infused with live data.
-
7:44
Let's redeploy and check it out.
-
7:46
So if your boot run task is still running, go ahead and stop it.
-
7:50
And after a few moments,
-
7:52
you can rerun that bootRun task to reboot your server and redeploy your web app.
-
7:58
And it looks like I have a compiler error.
-
8:06
I see what happened here.
-
8:08
So this error right here says that I can't
-
8:12
leave this generic between these angle brackets here empty.
-
8:16
And why does it say that?
-
8:18
Well, it says that because in my build file, if I go to my build file,
-
8:22
build.gradle, I said I wanted my Java source code to
-
8:28
be compatible with Java five, that is version 1.5, and
-
8:33
I have to use source 7 or higher to enable the diamond operator.
-
8:39
That's the operator that allows us to leave that generic empty.
-
8:43
So, what I'm going to do is, well, I could do one of two things, I could remove
-
8:47
the source compatibility here, I could increase the source compatibility here, or
-
8:52
I could go back to this line right here, and actually add gif right there.
-
8:59
I'm simply actually going to delete the source compatibility.
-
9:02
Delete that.
-
9:03
We'll save it and now I'm going to rerun the bootRun task.
-
9:09
And there it looks like the compilation has succeeded.
-
9:13
And the web server's up and running.
-
9:14
My application is deployed.
-
9:16
Let's check it out.
-
9:19
So I'm going to go back to my main page.
-
9:22
There's the home page, we didn't touch it in this last task.
-
9:24
What we did touch, though, is the Categories page.
-
9:28
And there it is!
-
9:29
It's working!
-
9:31
We see that the template is actually displayed, though it has static HTML.
-
9:35
And from this, you can probably guess what your final task is going to be.
You need to sign up for Treehouse in order to download course files.
Sign up