Spring MVC – Interview question on ModelAndView and DispatcherServlet

In this articles, we will understand few tricky situation in Spring MVC i.e.;

  1. Returning null for ModelAndView object
  2. Configuring multiple DispatcherServlet in web.xml

Case 1: Returning NULL for ModelAndView

What happens, if we return NULL object for ModelAndView in Spring MVC request flow

Spring MVC request flow:

  1. DispatcherServlet is the central servlet; which handles every incoming HTTP requests in application
  2. DispatcherServlet consults handler mapping before invoking associated @Controller with the incoming requests
  3. One of the associated @Controller method with respective mapping –> processes the HTTP requests
  4. After processing, ModelAndView is constructed and given back to DispatcherServlet again
  5. DispatcherServlet sends view-name from ModelAndView object to view resolver
  6. View-name along with model in ModelAndView is rendered and finally result shown to end-users

Tricky situation: What happens, if we return NULL object instead of returning of valid well-constructed ModelAndView object in above described Spring MVC request flow

To demonstrate this situation, we will re-use one of the earlier articles on Spring MVC – XML based application

Steps to modify the code

  • Step 1: Go through above Spring MVC application
  • Step 2: Modify the method as below i.e.; return null instead of actual ModelAndView object

MyFirstMVController.java

package com.spring.series.mvc.controller;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.AbstractController;

public class MyFirstMVController extends AbstractController {

	// http://localhost:8081/SpringMVC-XMLBased/firstmvc.htm

	@Override
	protected ModelAndView handleRequestInternal(HttpServletRequest httpServletRequest,
			HttpServletResponse httpServletResponse) throws Exception {

		ModelAndView modelAndView = new ModelAndView("showMessage");
		modelAndView.addObject("message", "My First Spring MVC web application");
		return null;
	}
}
Spring_MVC_return_NULL

Conclusion: When we access the URL,

  • It is perfectly alright to return NULL for ModelAndView object
  • There are no errors or exceptions thrown at server
  • There is no 404 page not found error
  • But response rendered is empty or blank as you can see in the above screen capture

Case 2: If there are multiple DispatcherServlet configured in web.xml

Absolutely, it is fine to configure 2 different DispatcherServlet in the same web.xml for any Spring MVC based application

web.xml

<?xml version="1.0" encoding="ISO-8859-1"?>
<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_2_5.xsd"
	id="WebApp_ID" version="2.5">

	<!-- Spring MVC DispatcherServlet: dispatches HTTP requests to registered 
		controllers -->
	<servlet>
		<servlet-name>mvc-dispatcher</servlet-name>
		<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
		<load-on-startup>1</load-on-startup>
	</servlet>

	<servlet>
		<servlet-name>mvc-dispatcher-2</servlet-name>
		<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
		<load-on-startup>1</load-on-startup>
	</servlet>

	<servlet-mapping>
		<servlet-name>mvc-dispatcher</servlet-name>
		<url-pattern>/first/*</url-pattern>
	</servlet-mapping>

	<servlet-mapping>
		<servlet-name>mvc-dispatcher-2</servlet-name>
		<url-pattern>/services/*</url-pattern>
	</servlet-mapping>

	<!-- location of the root application context xml file -->
	<context-param>
		<param-name>contextConfigLocation</param-name>
		<param-value>/WEB-INF/mvc-dispatcher-servlet.xml,
			/WEB-INF/mvc-dispatcher-2-servlet.xml</param-value>
	</context-param>

	<!-- context loader listener -->
	<listener>
		<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
	</listener>

</web-app>

From Spring official documentation,

A web application can define any number of DispatcherServlets. Each servlet will operate in its own namespace, loading its own application context with mappings, handlers, etc. Only the root application context as loaded by ContextLoaderListener, if any, will be shared.

As of Spring 3.1, DispatcherServlet may now be injected with a web application context, rather than creating its own internally. This is useful in Servlet 3.0+ environments, which support programmatic registration of servlet instances. See the DispatcherServlet(WebApplicationContext) javadoc for details.

References:

Happy Coding !!
Happy Learning !!

Java - How to construct a singleton class in a multi-threaded environment ?
Java - ArrayList v/s LinkedList