Spring MVC is mostly used with Spring for any web application development. It is very powerful and nice layered architecture for flow and configuration. It is very flexible to integrate with other web frameworks like struts.

In Spring Web MVC, DispatcherServlet class works as the front controller. It is responsible to manage the flow of the spring mvc application.

The @Controller annotation is used to mark the class as the controller in Spring 3.

The @RequestMapping annotation is used to map the request url. It is applied on the method.

Spring MVC Architecture

Spring MVC Execution Flow

Step 1: First request will be received by DispatcherServlet.

Step 2: DispatcherServlet will take the help of HandlerMapping and get to know the Controller class name associated with the given request.

Step 3: So request transfer to the Controller, and then controller will process the request by executing appropriate methods and returns ModelAndView object (contains Model data and View name) back to the DispatcherServlet.

Step 4: Now DispatcherServlet send the model object to the ViewResolver to get the actual view page.

Step 5: Finally DispatcherServlet will pass the Model object to the View page to display the result.

Now Let us understand Spring MVC flow with hello world application Example.

First of all, we create Dynamic web project in eclipse and following are the required files for the same:

  • JavaController.java
  • welcomePage.jsp
  • web.xml
  • welcome-servlet.xml
  • index.jsp

Practical Execution Flow

Step 1)Run the application, then index.jsp file will be executed then click on the link given (I have given <a href=”codeNuclear.html”>Click here to check the output.</a>).

Step 2)Once you click on that link, container will check the URL pattern at web.xml and passes the request to the DispatcherServlet.

Step 3)DispatcherServlet then passes that request to our controller class.

Step 4)DispatcherServlet verifies this ‘codeNuclear’ name with the string in @RequestMapping(“-“) in our controller class if same it will executes the ModelAndView method, which gives ModelAndView object as return type.

Means first argument is ‘View’ page name [ Where we are sending our result ], second, third arguments are key,values.

Step 5)So DispatcherServlet search for the name welcomePage in /jsp folder with extension .jsp , once the file was opened you can access the data by using the key welcomeMessage [2nd parameter in ModelAndView object].

Check welcomePage.jsp and printing the result by calling the key ${welcomeMessage} from welcomePage.jsp.


That’s it :-) Keep Learning…!!!

10 Thoughts on “Spring MVC flow with Example”

Leave a Reply

Your email address will not be published. Required fields are marked *