Configure multiple View Resolvers in Spring

1. Introduction

In Spring, the View Resolver is provided to resolve the view with the data available in the model, without tightly binding to a View technology, be it JSP, Velocity or Thymeleaf. Spring makes it easy and flexible to configure one or multiple View Resolvers, as per the need would be.

2. Spring MVC application flow

Before we proceed with understanding how multiple View Resolvers serve the purpose, lets take a quick recap of the Spring MVC application flow.

  1. Incoming request comes through web.xml, dispatcher servlet and hits the controller.
  2. Controller interacts with the application layers and prepares the model.
  3. Controller returns the ModelAndView, with model and the view name.
  4. The ViewResolver provides a mapping between view names and actual views.
  5. The View interface addresses the request of a view to respective View technology.
  6. The view is then rendered onto the browser along with the model data.

3. Implementation

Let’s start with the pom dependencies.


pom.xml

Not much change in the web.xml.

web.xml

Here is the mvc-dispatcher-servlet, which has multiple View Resolvers defined.

mvc-dispatcher-servlet.xml

Note that the two View Resolvers configured are InternalResourceViewResolver and BeanNameViewResolver. We have also set the priorities using the order property of the View Resolver. So BeanNameViewResolver has a higher priority. This means that when the ModelAndView object is returned, the BeanNameViewResolver checks for the available bean views with the view name that is returned. If the matching bean view is found, it is rendered. If not, the next View Resolver i.e. InternalResourceViewResolver, comes into the picture, and similarly checks for the JSPs with the view name that is returned with ModelAndView. If it is found, the view is rendered. But if not, and there is no more View Resolvers down the hierarchy, an appropriate exception is thrown.

Moving ahead with the implemetation, we have the same Employee entity class, as we have been using recently.

Employee.java

Let’s now write down the EmployeeController.

EmployeeController.java

We now make sure the JSP with the view name – employeeDetails, exists.

employeeDetails.jsp

4. Running the application

When we run the application, below is what we see.

Multiple View Resolvers in Spring

Note that we don’t have any bean view with name employeeDetails, but rather matches with the actual JSP view file employeeDetails.jsp. Hence, the view gets resolved as JSP.

Now let’s return the view name as jsonView, modifying the below statement in the controller method.

BeanNameViewResolver being higher in priority, this time finds the bean view with the name jsonView, and instead of rendering the JSP view, the same URI now returns a JSON.

Multiple View Resolvers in Spring - JSON

5. Download the source code

Receive our updates to your inbox

Get more stuff like this
in your inbox

Subscribe to our mailing list and get interesting stuff and updates to your email inbox.