@RequestMapping at the class level

The Spring MVC allows you to use the @RequestMapping annotation at the class level. This means we can annotate the controller class with @RequestMapping, as shown in the following code snippet:

    package com.packt.patterninspring.chapter10.bankapp.web.controller; 
 
    import org.springframework.stereotype.Controller; 
    import org.springframework.ui.ModelMap; 
    import org.springframework.web.bind.annotation.RequestMapping; 
    import org.springframework.web.bind.annotation.RequestMethod; 
 
    @Controller 
    @RequestMapping("/") 
    public class HomeController { 
    
     @RequestMapping(method=GET) 
     public String home() { 
         return "home"; 
     } 
   } 

As you have seen in the preceding code, the HomeController class is annotated with the @RequestMapping and @Controller annotations. But the HTTP method is still defined above the handler methods. Class-level mapping is applied with all the handler methods defined under this controller.

After the Spring MVC configuration, we created a controller class with the handler methods. Let's test this controller before moving ahead with more details. In this book, I haven't use any JUnit test cases, so here, I will just run this web application on the Tomcat container. You can see the output on the browser as follows:

The last image is the homepage of our Bank Management System web application.

Before Spring 3.1, the Spring MVC mapped the requests to handler methods using two steps. First, the controller was selected by DefaultAnnotationHandlerMapping, and then, the actual method was mapped with the incoming requests by the AnnotationMethodHandlerAdapter. But as of Spring 3.1, Spring MVC maps the requests, in one step, directly to the handler methods by using RequestMappingHandlerMapping.

In the next section, we'll see how to define the handler methods, and the return type and parameters allowed for the handler methods in Spring MVC.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset