Spring MVC Tutorial

Core Spring MVC

Spring MVC - Annotation

Spring MVC - Form Handling

Spring MVC with JSTL

Spring MVC with REST API

Spring MVC with Database

Spring MVC - Multiple Controller

In a typical Spring MVC application, having multiple controllers is quite common. Each controller can manage a specific set of related functionalities. Splitting functionalities across different controllers helps in organizing the codebase and making it more maintainable.

Let's take an example of an e-commerce application. In such an application, you might have different functionalities like managing users, managing products, handling orders, etc. Each of these functionalities can be managed by a separate controller.

Example:

1. User Controller:

@Controller
@RequestMapping("/user")
public class UserController {

    @GetMapping("/profile")
    public String userProfile(Model model) {
        // fetch user details and set in model
        return "userProfile";
    }

    @GetMapping("/orders")
    public String userOrders(Model model) {
        // fetch user orders and set in model
        return "userOrders";
    }
}

2. Product Controller:

@Controller
@RequestMapping("/product")
public class ProductController {

    @GetMapping("/list")
    public String productList(Model model) {
        // fetch list of products and set in model
        return "productList";
    }

    @GetMapping("/details")
    public String productDetails(@RequestParam("id") int productId, Model model) {
        // fetch product details based on productId and set in model
        return "productDetails";
    }
}

3. Order Controller:

@Controller
@RequestMapping("/order")
public class OrderController {

    @GetMapping("/recent")
    public String recentOrders(Model model) {
        // fetch recent orders and set in model
        return "recentOrders";
    }

    @PostMapping("/place")
    public String placeOrder(@ModelAttribute Order order, Model model) {
        // process the order and maybe set some confirmation details in model
        return "orderConfirmation";
    }
}

Key Points:

  • Each controller is annotated with @Controller, making it a Spring-managed bean that can handle incoming HTTP requests.

  • The @RequestMapping annotation at the class level defines a base path for all methods in that controller. This helps to group related endpoints together.

  • Within each controller, methods are further annotated with more specific request mappings (@GetMapping, @PostMapping, etc.) to handle different URLs and HTTP methods.

  • Controllers can share services, components, and other beans by autowiring them as needed.

Splitting functionalities across multiple controllers not only provides better organization but also makes the code easier to understand for developers who might be working on specific parts of the application. It also makes the application more scalable, as developers can work on different controllers in parallel without much conflict.

  1. Spring MVC Multiple Controllers Example:

    • Description: This is a basic example showcasing the usage of multiple controllers in a Spring MVC application.

    • Code Snippet: (Controller)

      // Controller class 1
      @Controller
      public class HomeController {
      
          @RequestMapping("/home")
          public String home() {
              return "home";
          }
      }
      
      // Controller class 2
      @Controller
      public class ProductController {
      
          @RequestMapping("/products")
          public String products() {
              return "products";
          }
      }
      
  2. Using Multiple Controllers in Spring MVC:

    • Description: This example demonstrates how to use multiple controllers in a Spring MVC application to handle different parts of the application.

    • Code Snippet: (Controllers)

      // HomeController
      @Controller
      public class HomeController {
      
          @RequestMapping("/home")
          public String home() {
              return "home";
          }
      }
      
      // ProductController
      @Controller
      public class ProductController {
      
          @RequestMapping("/products")
          public String products() {
              return "products";
          }
      }
      
  3. How to Configure Multiple Controllers in Spring MVC:

    • Description: This example shows how to configure multiple controllers in a Spring MVC application using Java configuration.

    • Code Snippet: (Configuration)

      // AppConfig class
      @Configuration
      @ComponentScan("com.example.controllers")
      public class AppConfig {
          // Additional configuration if needed
      }
      
  4. Working with Multiple Controllers and Views in Spring MVC:

    • Description: This example illustrates working with multiple controllers and views in a Spring MVC application to handle different parts of the user interface.

    • Code Snippet: (Controllers)

      // HomeController
      @Controller
      public class HomeController {
      
          @RequestMapping("/home")
          public String home() {
              return "home";
          }
      }
      
      // ProductController
      @Controller
      public class ProductController {
      
          @RequestMapping("/products")
          public String products() {
              return "products";
          }
      }
      
  5. Configuring Multiple Controller Classes in Spring MVC:

    • Description: This example showcases how to configure multiple controller classes in a Spring MVC application using XML configuration.

    • Code Snippet: (XML Configuration)

      <!-- applicationContext.xml -->
      <beans xmlns="http://www.springframework.org/schema/beans"
             xmlns:mvc="http://www.springframework.org/schema/mvc"
             xmlns:context="http://www.springframework.org/schema/context"
             xsi:schemaLocation="http://www.springframework.org/schema/beans
             http://www.springframework.org/schema/beans/spring-beans.xsd
             http://www.springframework.org/schema/mvc
             http://www.springframework.org/schema/mvc/spring-mvc.xsd
             http://www.springframework.org/schema/context
             http://www.springframework.org/schema/context/spring-context.xsd">
      
          <!-- Component scanning for controllers -->
          <context:component-scan base-package="com.example.controllers" />
      
          <!-- Additional configuration if needed -->
      
      </beans>
      
  6. Handling Multiple Endpoints with Spring MVC Controllers:

    • Description: This example demonstrates how to handle multiple endpoints with different controllers in a Spring MVC application.

    • Code Snippet: (Controllers)

      // HomeController
      @Controller
      public class HomeController {
      
          @RequestMapping("/home")
          public String home() {
              return "home";
          }
      }
      
      // ProductController
      @Controller
      public class ProductController {
      
          @RequestMapping("/products")
          public String products() {
              return "products";
          }
      }
      
  7. Organizing URL Mappings for Multiple Controllers in Spring:

    • Description: This example showcases organizing URL mappings for multiple controllers in a Spring MVC application to maintain a clean and structured architecture.

    • Code Snippet: (Controllers)

      // HomeController
      @Controller
      @RequestMapping("/home")
      public class HomeController {
      
          @RequestMapping("/dashboard")
          public String dashboard() {
              return "dashboard";
          }
      }
      
      // ProductController
      @Controller
      @RequestMapping("/products")
      public class ProductController {
      
          @RequestMapping("/list")
          public String productList() {
              return "productList";
          }
      }
      
  8. Common Patterns for Multiple Controllers in Spring MVC:

    • Description: This example explores common patterns for organizing and using multiple controllers in a Spring MVC application.

    • Code Snippet: (Controllers)

      // HomeController
      @Controller
      @RequestMapping("/home")
      public class HomeController {
      
          @RequestMapping("/dashboard")
          public String dashboard() {
              return "dashboard";
          }
      }
      
      // ProductController
      @Controller
      @RequestMapping("/products")
      public class ProductController {
      
          @RequestMapping("/list")
          public String productList() {
              return "productList";
          }
      }
      
  9. Managing Dependencies Between Multiple Controllers in Spring:

    • Description: This example demonstrates how to manage dependencies between multiple controllers in a Spring MVC application.

    • Code Snippet: (Controllers)

      // HomeController
      @Controller
      public class HomeController {
      
          private final ProductService productService;
      
          @Autowired
          public HomeController(ProductService productService) {
              this.productService = productService;
          }
      
          // Controller methods using productService
      }
      
      // ProductController
      @Controller
      public class ProductController {
      
          private final ProductService productService;
      
          @Autowired
          public ProductController(ProductService productService) {
              this.productService = productService;
          }
      
          // Controller methods using productService
      }