Spring MVC XmlViewResolver Configuration Example

In Spring MVC based application, the last step of request processing is to return the logical view name. Here DispatcherServlet has to delegate control to a view template so the information is rendered. This view template decides that which view should be rendered based on returned logical view name. These view templates are one or more view resolver beans declared in the web application context. These beans have to implement the ViewResolver interface for DispatcherServlet to auto-detect them. Spring MVC comes with several ViewResolver implementations. In this example, we will look at such a view resolver template i.e. XmlViewResolver.

Contrary to InternalResourceViewResolver where each logical view name is mapped to physical location of view directly, in case of XmlViewResolver, views are declared as Spring beans. You can declare the view beans in the same configuration file as the web application context, but it’s better to isolate them in a separate configuration file.

By default, XmlViewResolver loads view beans from /WEB-INF/views.xml, but this location can be overridden through the location property.

<bean class="org.springframework.web.servlet.view.XmlViewResolver">
    <property name="location">
        <value>/WEB-INF/admin-views.xml</value>
    </property>
</bean>

In the admin-views.xml configuration file, you can declare each view as a normal Spring bean by setting the class name and properties. e.g.

<beans xmlns="http://www.springframework.org/schema/beans"
    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">

    <bean id="home" class="org.springframework.web.servlet.view.JstlView">
        <property name="url" value="/WEB-INF/jsp/home.jsp" />
    </bean>

    <bean id="admin/home" class="org.springframework.web.servlet.view.JstlView">
        <property name="url" value="/WEB-INF/jsp/admin/home.jsp" />
    </bean>

    <bean id="logOffRedirect" class="org.springframework.web.servlet.view.RedirectView">
        <property name="url" value="home" />
    </bean>
</beans>

The first two beans in above configuration are pretty obvious. Logical view name “home” is mapped to “/WEB-INF/jsp/home.jsp” and “admin/home” is mapped to “/WEB-INF/jsp/admin/home.jsp“.

The third bean do not map any physical view file, rather it redirect the request to url “home” which is actually handled by controller of URL “/home“. Whatever logical name that controller will return, that view will be looked up into bean mappings and then actual view file will be obtained.

Drop me your queries if something needs more explanation.

Happy Learning !!

Was this post helpful?

Join 7000+ Fellow Programmers

Subscribe to get new post notifications, industry updates, best practices, and much more. Directly into your inbox, for free.

4 thoughts on “Spring MVC XmlViewResolver Configuration Example”

  1. Hi Lokesh,

    How are you? I hope well!

    I have a question about a problem related with xmlViewResolver.
    The question is about changing the location of the view.xml file to another folder, instead WEB-INF could be src/main/resources/. This is because I’m changing to spring boot in a jar, so there will be no WEB-INF folder when I deploy this jar. The thing is that I created config java class with the bean xmlViewResolver but when I set the location with a resource loader and I try to use this way of mapping to generate a excel file via location.href it says error 404.

    Thank you in advance for anything you could think of.
    Fabio

  2. I have a question. for the id of bean and the jsp page can be defferent or not?
    for example with the bean id home and /WEB-INF/jsp/home.jsp. the bean id home replace or rename by home1 or not?

  3. I am developing a spring rest service. I would like to remove the fields with null values on the fly using spring configuration while returning response. Please let me know how can we achieve this, but no use.

    I added the below config in context xml:

Comments are closed.

HowToDoInJava

A blog about Java and its related technologies, the best practices, algorithms, interview questions, scripting languages, and Python.