From the course: Spring Web MVC 6

MVC controller annotations - Spring Tutorial

From the course: Spring Web MVC 6

MVC controller annotations

- [Instructor] We implemented the homepage flow and wrote a controller class in our previous module. So by now, we know that the C in Spring MVC is the controller component that you write in your apps. And in order to mark any class as controller, we always annotate it with the @Controller annotation that goes right on top of the class. There is a host of annotations that you can use in your controller classes methods. There's also some objects. Let's take a look at few of them right now. Depending on the kind of request that you make from the UI, it could be a Get, Post, Put, or Delete, you have corresponding annotations, @GetMapping, @PostMapping, @PutMapping, and @DeleteMapping. This is so that you can map your URL paths to the controller methods. We also saw a path variable annotation which is used to capture the path variables that you have in your incoming URL. There is another one that is called @RequestParam. So as the name suggests, it is used to capture the request parameters inside your controller methods. We will look at this @RequestParam annotation in the search use case that we will implement next. And in addition to this, there is also this big one, which is called Model. The Model API from the spring MVC framework helps you to store any data that you want to navigate into on the UI. Now, let us understand how we'll implement the search use case for this website. So if you go back to our website, this is the display and you have the search icon right there. When you click, you are going to enter some search string and when you click on the search button, it is going to display the relevant search results. So how we do this is, you'll have the search icon in UI. From there, we are going to send a request and that request lands in controller method. This controller will capture that search string, or the keyword that was entered by the user, and then give it for further processing. The controller then forwards that search string to the data access layer which in turn will talk to the database. So the data access is supposed to search the keyword from the database and get all the relevant results. Once we fetch the results, then the flow just goes back to the controller and then back to the UI where we'll display the search results. So that is a use case that we're going to implement next.

Contents