Controller Method for Adding a Category3:29 with Chris Ramacciotti
We now turn our attention to the CategoryController, coding the method that will capture the form submission when a user makes a request to add a category. Here we discuss how Spring intelligently uses controller method parameters (a Category in our case) to reconstruct submitted data into an entity object to be saved.
Git Command to Sync Your Code to the Start of this Video
git checkout -f s3v3
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.
For processing the addition of a new category we'll use an HTTP Post Request. 0:00 Remember from HTTP basics that these requests Post Requests 0:04 are used to make changes on the server. 0:09 Because we aren't updating an existing category here, 0:11 we'll use a Post Request to the same URI that is used for a category index. 0:14 Let me show you in the category controller. 0:18 If i use IntelliJ structure view, we can get a quick overview of the methods. 0:22 So if I hop up to list categories you see the URI that this is capturing 0:27 is /categories. 0:31 Now the default request method in the @RequestMapping annotation 0:33 if it's not listed here is going to be a GET Request. 0:37 If I go down to addCategory which is the controller method that I'm to add 0:42 a new category that is to process the form submission for adding a new category. 0:48 I see that we are submitting to the same URI here, 0:53 /categories, as we did back up in list categories. 0:57 The difference here is that the method that is listed is a POST and 1:00 here in lies the difference between POST and GET Requests. 1:05 A POST Request will make a change to the server, a GET Request according 1:09 to HTTP specifications should not make changes to the server. 1:14 Now, in this add category method, we'll need to call the auto wired category 1:18 services save method passing the category object that was submitted. 1:24 So under this to do comment I will do just that. 1:29 CategoryService.save and then the category. 1:32 If you're wondering about the part of this comment that refers to valid data, 1:39 we ll be returning to this in the next stage. 1:43 And as for this category variable, well, we don't have one yet. 1:46 So where will this come from? 1:50 Well, this will be assembled via the form data that is submitted, 1:52 which results in a call to this controller method. 1:55 And because we'll assume that the form data submitted will include all necessary 1:58 fields for a category object to be constructed. 2:02 We can have spring reassemble that form data into a category object by 2:05 sticking a category parameter in the method just like this. 2:10 Category, category. 2:14 Now, after the posted category is saved, 2:18 we want to redirect back to the category index view. 2:21 This is our implementation of the post redirect get pattern and 2:24 it looks like this. 2:27 So here, we will redirect to /categories as a URI. 2:29 Now, when Spring sees a view name beginning with redirect followed by 2:36 a colon, it will send a 302 response code along with 2:39 the location header to /categories. 2:44 It's important to understand here that whatever you specify after this colon 2:49 right here, is what the browser will redirect to and 2:53 not the name of a view to render. 2:58 Also important here, 3:00 is that the redirect will be a GET Request which means whichever controller method 3:01 captures the /categories URI using the request method Get. 3:06 That is the controller method that will execute in our case 3:13 list categories does just that. 3:17 So the end effect of this is that will save a category and then the browser will 3:20 redirect to the /categories view which is the category index view 3:24
You need to sign up for Treehouse in order to download course files.Sign up