Sunday, September 15, 2019

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

  1.     <!-- https://mvnrepository.com/artifact/org.springframework/spring-webmvc -->  
  2. <dependency>  
  3.     <groupId>org.springframework</groupId>  
  4.     <artifactId>spring-webmvc</artifactId>  
  5.     <version>5.1.1.RELEASE</version>  
  6. </dependency>  
  7.   
  8. <!-- https://mvnrepository.com/artifact/javax.servlet/javax.servlet-api -->  
  9. <dependency>    
  10.     <groupId>javax.servlet</groupId>    
  11.     <artifactId>servlet-api</artifactId>    
  12.     <version>3.0-alpha-1</version>    
  13. </dependency>  

2. Create the request page

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

index.jsp

  1. <html>  
  2. <body>  
  3. <a href="hello1">Spring MVC</a> ||  
  4. <a href="hello2">Spring Boot</a>  
  5. </body>  
  6. </html>  

3. Create the controller class

Let's create two controller classes, where each returns the particular view page.
HelloController1.java
  1. package com.javatpoint;  
  2. import org.springframework.stereotype.Controller;  
  3. import org.springframework.web.bind.annotation.RequestMapping;  
  4. @Controller  
  5. public class HelloController1 {  
  6. @RequestMapping("/hello1")  
  7.     public String display()  
  8.     {  
  9.         return "viewpage1";  
  10.     }     
  11. }  
HelloController2.java
  1. package com.javatpoint;  
  2. import org.springframework.stereotype.Controller;  
  3. import org.springframework.web.bind.annotation.RequestMapping;  
  4. @Controller  
  5. public class HelloController2 {  
  6. @RequestMapping("/hello2")  
  7.     public String display()  
  8.     {  
  9.         return "viewpage2";  
  10.     }     
  11. }  

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

web.xml
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">  
  3.   <display-name>SpringMVC</display-name>  
  4.    <servlet>    
  5.     <servlet-name>spring</servlet-name>    
  6.     <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>    
  7.     <load-on-startup>1</load-on-startup>      
  8. </servlet>    
  9. <servlet-mapping>    
  10.     <servlet-name>spring</servlet-name>    
  11.     <url-pattern>/</url-pattern>    
  12. </servlet-mapping>    
  13. </web-app>  

5. Define the bean in the xml file

spring-servlet.xml
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <beans xmlns="http://www.springframework.org/schema/beans"  
  3.     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"   
  4.     xmlns:context="http://www.springframework.org/schema/context"  
  5.     xmlns:mvc="http://www.springframework.org/schema/mvc"  
  6.     xsi:schemaLocation="  
  7.         http://www.springframework.org/schema/beans  
  8.         http://www.springframework.org/schema/beans/spring-beans.xsd  
  9.         http://www.springframework.org/schema/context  
  10.         http://www.springframework.org/schema/context/spring-context.xsd  
  11.         http://www.springframework.org/schema/mvc  
  12.         http://www.springframework.org/schema/mvc/spring-mvc.xsd">  
  13.   
  14.     <!-- Provide support for component scanning -->  
  15.     <context:component-scan base-package="com.javatpoint" />  
  16.   
  17.     <!--Provide support for conversion, formatting and validation -->  
  18.     <mvc:annotation-driven/>  
  19. <bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">  
  20.         <property name="prefix" value="/WEB-INF/jsp/"></property>  
  21.         <property name="suffix" value=".jsp"></property>          
  22.      </bean>  
  23. </beans>  

6. Create the other view components

viewpage1.jsp
  1. <html>  
  2. <body>  
  3. <p>Welcome to Spring MVC Tutorial</p>  
  4. </body>  
  5. </html>  
viewpage1.jsp
  1. <html>  
  2. <body>  
  3. <p>Welcome to Spring Boot Tutorial</p>  
  4. </body>  
  5. </html>  
Output:
Spring MVC Multiple Controller
Spring MVC Multiple Controller
Spring MVC Multiple Controller

No comments:

Post a Comment

How to DROP SEQUENCE in Oracle?

  Oracle  DROP SEQUENCE   overview The  DROP SEQUENCE  the statement allows you to remove a sequence from the database. Here is the basic sy...