Spring MVC Multiple Controller




Hi Friends , My today's post is on usage of Multiple Controller in one application . Till the examples I have shown use only one controller .If you haven't gone through simple examples like Hello World Example in Spring MVC, Spring MVC validations and Spring Flow please first go through them otherwise it ll be difficult for you to understand .

In the example below I am going to create 2 controllers with different @RequestMapping to show how the request ll flow:

Step 1: Controller classes:

package com.javainhouse;  
import org.springframework.stereotype.Controller;  
import org.springframework.web.bind.annotation.RequestMapping;  
import org.springframework.web.servlet.ModelAndView;  
  
@Controller  
public class HelloWorldController {  
  
    @RequestMapping("/hello")  
    public ModelAndView helloWorld() {  
  
        String message = "Inside Hello Controller";  
        return new ModelAndView("hellopage", "message", message);  
    }  
      
}  


and the other controller

package com.javainhouse;  
import org.springframework.stereotype.Controller;  
import org.springframework.web.bind.annotation.RequestMapping;  
import org.springframework.web.servlet.ModelAndView;  
  
@Controller  
public class WelcomeController {  
  
    @RequestMapping("/welcome")  
    public ModelAndView helloWorld() {  
  
        String message = "Inside Welcome Controller";  
        return new ModelAndView("welcomepage", "message", message);  
    }  
      
}  


Step 2: Views of application 

index.jsp
<a href="hello.html">Hello World Controller</a>
<a href="welcome.html">Welcome Controller</a> 

hellopage.jsp

Message is: ${message}

welcomepage.jsp

Message is: ${message}

Note : These views should be located under WEB/INF folder and hellopage.jsp and welcomepage.jsp should be under jsp folder under WEB/INF folder as we have given /jsp in view resolver .For proper servlet.xml ,spring .xml please go through Hello World tutorial.


Explanation :
 This is very simple program ,If you have already gone through my previous programs . As in -servlet.xml we have given the basic package in which we have defined both controllers in package com.javainhouse. Request with hello.html link ll go to HelloWorldController and welcome ll go to Welcome Controller .

Thats it for this program .Please provide suggestions and errors in the comments below :)

Spring MVC Introduction and Flow

 

Spring MVC is based on the very famous architecture MVC i.e.  three parts Model ,View and Controller . But the hero of this movie is some one else which is Dispatcher servlet .


So , in very simple words we can define these three parts as :

Model – You can Say POJO classes in you r application .

View –  jsp ,html and other presentable content of your application .

Controller – is also a java class but as the name specify it control the whole application flow means it handles the user request and decide what to do next ?


So why did I say that Dispatcher servlet is the hero ? It should be controller

Dispatcher servlet is the first thing in our application to which our request hits and then it is decided further that which controller will work upon it . So Dispatcher servlet is the hero of movie .

SpringMVCFLow


Now let us move to the whole flow of Spring MVC application .

1. When a user make a request to Spring MVC application it first goes o Dispatcher servlet .

2.Dispatcher Servlet is defined in web.xml of an application .This is the very first servlet invoked in an application.

3. Then ,Dispatcher Servlet asks Handler Mapper that which controller needs to be invoke for this particular request .

4. Handler Mapper invokes the appropriate Controller.

5. Now the controller takes  request and invokes the appropriate  GET or POST method and populates model data using the business logic defined and return the view name to Dispatcher Servlet .

6. Dispatcher Servlet take that view name and take help of view resolver to find the exact directory structure of the view i.e. jsp or html page .

7. At the end when view is resolved the model data is passed by Dispatcher Servlet to the View .

8. And now User can see the response of the request sent means the View is the response.