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
1. Add dependencies to pom.xml
-
- <dependency>
- <groupId>org.springframework</groupId>
- <artifactId>spring-webmvc</artifactId>
- <version>5.1.1.RELEASE</version>
- </dependency>
-
-
- <dependency>
- <groupId>javax.servlet</groupId>
- <artifactId>servlet-api</artifactId>
- <version>3.0-alpha-1</version>
- </dependency>
2. Create the request page
Let's create a simple JSP page containing two links.
index.jsp
- <html>
- <body>
- <a href="hello1">Spring MVC</a> ||
- <a href="hello2">Spring Boot</a>
- </body>
- </html>
3. Create the controller class
Let's create two controller classes, where each returns the particular view page.
HelloController1.java
- package com.javatpoint;
- import org.springframework.stereotype.Controller;
- import org.springframework.web.bind.annotation.RequestMapping;
- @Controller
- public class HelloController1 {
- @RequestMapping("/hello1")
- public String display()
- {
- return "viewpage1";
- }
- }
HelloController2.java
- package com.javatpoint;
- import org.springframework.stereotype.Controller;
- import org.springframework.web.bind.annotation.RequestMapping;
- @Controller
- public class HelloController2 {
- @RequestMapping("/hello2")
- public String display()
- {
- return "viewpage2";
- }
- }
4. Provide the entry of controller in the web.xml file
web.xml
- <?xml version="1.0" encoding="UTF-8"?>
- <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">
- <display-name>SpringMVC</display-name>
- <servlet>
- <servlet-name>spring</servlet-name>
- <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
- <load-on-startup>1</load-on-startup>
- </servlet>
- <servlet-mapping>
- <servlet-name>spring</servlet-name>
- <url-pattern>/</url-pattern>
- </servlet-mapping>
- </web-app>
5. Define the bean in the xml file
spring-servlet.xml
- <?xml version="1.0" encoding="UTF-8"?>
- <beans xmlns="http://www.springframework.org/schema/beans"
- xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
- xmlns:context="http://www.springframework.org/schema/context"
- xmlns:mvc="http://www.springframework.org/schema/mvc"
- xsi:schemaLocation="
- http://www.springframework.org/schema/beans
- http://www.springframework.org/schema/beans/spring-beans.xsd
- http://www.springframework.org/schema/context
- http://www.springframework.org/schema/context/spring-context.xsd
- http://www.springframework.org/schema/mvc
- http://www.springframework.org/schema/mvc/spring-mvc.xsd">
-
-
- <context:component-scan base-package="com.javatpoint" />
-
-
- <mvc:annotation-driven/>
- <bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
- <property name="prefix" value="/WEB-INF/jsp/"></property>
- <property name="suffix" value=".jsp"></property>
- </bean>
- </beans>
6. Create the other view components
viewpage1.jsp
- <html>
- <body>
- <p>Welcome to Spring MVC Tutorial</p>
- </body>
- </html>
viewpage1.jsp
- <html>
- <body>
- <p>Welcome to Spring Boot Tutorial</p>
- </body>
- </html>
Output:
No comments:
Post a Comment