Spring MVC Multiple Controller Example

In Spring MVC, we can create multiple controllers at a time. It is required to map each controller class with @Controller annotation. Here, we see a Spring MVC example of multiple controllers. The steps are as follows:

  • Load the spring jar files or add dependencies in the case of Maven
  • Create the controller class
  • Provide the entry of controller in the web.xml file
  • Define the bean in the separate XML file
  • Create the other view components
  • Start the server and deploy the project

Directory Structure of Spring MVC

Spring MVC Multiple Controller

1. Add dependencies to pom.xml

2. Create the request page

Let's create a simple JSP page containing two links.

index.jsp

3. Create the controller class

Let's create two controller classes, where each returns the particular view page.

HelloController1.java

HelloController2.java

4. Provide the entry of controller in the web.xml file

web.xml

5. Define the bean in the xml file

spring-servlet.xml

6. Create the other view components

viewpage1.jsp

viewpage1.jsp

Output:

Spring MVC Multiple Controller
Spring MVC Multiple Controller
Spring MVC Multiple Controller
Next TopicModel Interface




Contact US

Email:[email protected]

Multiple Controller
10/30