When a web page or other client-side asset sends a request to a controller, the particulars of that controller can be so varied. The request may be very simple and sending no data params along with the request or the request may fall in any number of places on the degrees of complexity scale, such as a request sending in form data and certain request headers that impact how the request should be dispatched, processed, and responded to. This video breaks down the anatomy of some common request types and introduces the many choices you have as a Spring MVC developer for using the appropriate request mappings in your controllers.
- [Instructor] So remember that features wish list we had for our sample application? Well here it is again for review because we're going to pick one easy feature to get us started. Our first controller that we're going to implement is the Request an Estimate controller. We want to apply the handler logic that allows a customer to come to the Landon Hotel website, and ask for an estimate for the event that they're planning. This will involve them filling in a simple form that gets sent of to the event sales team.
So let's get started. To begin, I've already started creating a class the QuoteRequestController. Notice how this class, it's just a simple class that I made, and I don't have to worry about adding any Spring specific aspects to it. So to make this a controller is actually a pretty simple two-step process. First, I add the Controller annotation to the top of the class.
Right there alone I have a controller, but I don't have any handler mappings just yet. There's no way for a user's request to come in and actually be mapped to this controller class. So, step two let's start adding some handler methods. The first thing I want to add is a GetMapping. When a user first comes to the page where they want to fill in a quote form they first have to get that page.
So let me add my GetMapping to this method, beginQuoteRequest. Now that I've added my mapping, the only other thing I want to specify at this time is, let's say I add some implementation here which will come later. The next thing I want to do is just specify when I'm done with all that implementation what do I want to return to the user? Well in spring MVC I get to supply what's called a logical view name which is just a string.
I'm calling it newQuote. This tends to correlate to some HTML or JSP or whatever type of files you're using for your view. For now, it's sufficient to just put newQuote and we'll explain what that means later. Now a user can get a quote. But once they fill in that quote with their name, and other event details, they may want to save or post that quote. To do that, I do have to add a PostMapping and a handler for that post.
That handler needs to be a separate method. I'm calling this method submitQuoteRequest, and once that quote is submitted, I want to return the user to a view that for now I'm just going to call it the newQuoteConfirmation. So you can imagine this will just be a separate page with a confirmation message that lets the user know that their quote request was received. And again I'm going to add a little note to myself here, that we will add some implementation later.
But for the most part, this is all that's needed to make a controller. Your controller annotations, your handler methods, and then the mappings for those methods. We've started out with two of the most popular mappings, a @GetMapping and a @PostMapping. Alright, so I'm going to add implementation later. But, in the interest of being a responsible developer, I'm going to also show you one other step I took.
Because I don't really have a view in place yet, and I don't have a model, and I don't have a lot of things, this is good time to start writing unit test, and practicing a habit called test driven development. Now there is other courses in the library you can take to learn more about test driven development especially for the Spring MVC framework. So right now I've already started a unit test. That lets me just test the basic plumbing and the flow that I've put in place for both my @GetMapping and my @PostMapping.
You'll see my first unit test testGetQuoteForm. I just want to show here that whenever a user requests that new quote screen, that the correct controller is called and an OK status is returned to the user. So I just want to demonstrate that I can run this test and that it will succeed, and that's really all I need to get started in a test-driven development flow, that lets me focus on implementing the controller, and writing tests, and not having to worry about all of the other components that aren't done yet.
So as you can see, that test succeeded. I got the green bar that I like to see after I run a Junit. From this point on, I can start adding implementation, and unit testing and working in that cycle until I'm done with that controller. So let's recap what we just did. We created a controller, it was just a plain-old Java class. All we had to do was add the @Controller annotation to the class. To work on our handler mappings, we just had to add some methods.
The two most popular methods you'll use most will handle your @GetMapping and your @PostMapping, we added those. And this is what the code looked like through each stage. We added our beginQuoteRequest to handle any GetRequest for the new quote URL. We added our sumbitQuoteRequest to handle any mappings to the new quote, in which the user the saving or posting data.
And this is the result. You put it all together and this is one complete yet very simple controller.
- Using Spring MVC for web projects
- Spring MVC controllers
- Creating controllers and views
- Spring MVC models
- Creating the model
- Request mapping
- Spring MVC views
- Custom app configuration