Bummer! This is just a preview. You need to be signed in with a Basic account to view the entire video.
Controller Method for Adding a Category3:29 with Unsubscribed User
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.
Remember from HTTP basics that these requests Post Requests
are used to make changes on the server.
Because we aren't updating an existing category here,
we'll use a Post Request to the same URI that is used for a category index.
Let me show you in the category controller.
If i use IntelliJ structure view, we can get a quick overview of the methods.
So if I hop up to list categories you see the URI that this is capturing
Now the default request method in the @RequestMapping annotation
if it's not listed here is going to be a GET Request.
If I go down to addCategory which is the controller method that I'm to add
a new category that is to process the form submission for adding a new category.
I see that we are submitting to the same URI here,
/categories, as we did back up in list categories.
The difference here is that the method that is listed is a POST and
here in lies the difference between POST and GET Requests.
A POST Request will make a change to the server, a GET Request according
to HTTP specifications should not make changes to the server.
Now, in this add category method, we'll need to call the auto wired category
services save method passing the category object that was submitted.
So under this to do comment I will do just that.
CategoryService.save and then the category.
If you're wondering about the part of this comment that refers to valid data,
we ll be returning to this in the next stage.
And as for this category variable, well, we don't have one yet.
So where will this come from?
Well, this will be assembled via the form data that is submitted,
which results in a call to this controller method.
And because we'll assume that the form data submitted will include all necessary
fields for a category object to be constructed.
We can have spring reassemble that form data into a category object by
sticking a category parameter in the method just like this.
Now, after the posted category is saved,
we want to redirect back to the category index view.
This is our implementation of the post redirect get pattern and
it looks like this.
So here, we will redirect to /categories as a URI.
Now, when Spring sees a view name beginning with redirect followed by
a colon, it will send a 302 response code along with
the location header to /categories.
It's important to understand here that whatever you specify after this colon
right here, is what the browser will redirect to and
not the name of a view to render.
Also important here,
is that the redirect will be a GET Request which means whichever controller method
captures the /categories URI using the request method Get.
That is the controller method that will execute in our case
list categories does just that.
So the end effect of this is that will save a category and then the browser will
redirect to the /categories view which is the category index view
You need to sign up for Treehouse in order to download course files.Sign up