Spring MVC multiple controller tutorial

Let us discuss spring mvc multiple controller example in eclipse.

Example Explanation:

Use http://localhost:8080/SpringMVCExample3/ url to start the application. When you click on any link, a request for respective resource will generate. Request will be handled by DispatcherServlet. The DispatcherServlet choose the controller with the help of HandlerMapping. It delegates the request to the specified controller. The specified controller resolve the request with help of RequestMapping annotation, executes the specific functionality and returns the ModelAndView object to the DispatcherServlet. The DispatcherServlet then take the help of InternalResourceViewResolver to get the actual view name. The DispatcherServlet then insert the model data into view and render response.

Example:

index.jsp

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <title>Spring Multiple Controller Example.</title>
  </head>
  <body>
    <a href="sayHello.html">Say Hello</a> 
    <a href="sayWelcome.html">Say Welcome</a> 
  </body>
</html>

web.xml

<?xml version="1.0" encoding="UTF-8"?>  
<web-app version="2.4"
   xmlns="http://java.sun.com/xml/ns/j2ee" 
   xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
   xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee 
   http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
 
   <servlet>
      <servlet-name>MultipleController</servlet-name>
      <servlet-class>
         org.springframework.web.servlet.DispatcherServlet
      </servlet-class>
      <load-on-startup>1</load-on-startup>
   </servlet>
 
   <servlet-mapping>
      <servlet-name>MultipleController</servlet-name>
      <url-pattern>*.html</url-pattern>
   </servlet-mapping>
 
</web-app>

MultipleController-servlet.xml

<?xml version="1.0" encoding="UTF-8"?>  
<beans xmlns="http://www.springframework.org/schema/beans"
   xmlns:context="http://www.springframework.org/schema/context"
   xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
   xsi:schemaLocation="
   http://www.springframework.org/schema/beans     
   http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
   http://www.springframework.org/schema/context 
   http://www.springframework.org/schema/context/spring-context-3.0.xsd">
 
   <context:component-scan base-package="com.w3spoint.business" />
 
   <bean class=
       "org.springframework.web.servlet.view.InternalResourceViewResolver">
      <property name="prefix" value="/WEB-INF/jsp/" />
      <property name="suffix" value=".jsp" />
   </bean>
 
</beans>

HelloController.java

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;
 
@Controller
public class HelloController {
   @RequestMapping("/sayHello")  
   public ModelAndView sayHello() {
      String message = "Spring MVC Hello World Example.";
      return new ModelAndView("helloWorld", "message", message);  
   }
}

WelcomeController.java

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;
 
@Controller
public class WelcomeController {
   @RequestMapping("/sayWelcome")  
   public ModelAndView sayHello() {
      String message = "Welcome to the world of spring.";
      return new ModelAndView("welcome", "message", message);  
   }	
}

helloWorld.jsp

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <title>Spring Multiple Controller Example.</title>
  </head>
  <body>
    <h2>${message}</h2>
  </body>
</html>

welcome.jsp

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <title>Spring Multiple Controller Example.</title>
  </head>
  <body>
    <h2>${message}</h2>
  </body>
</html>

Output:

Spring example2-1 Click on Say Welcome link. Spring example2-2   Download this example.  

Please follow and like us:
Content Protection by DMCA.com