HowToDoInJava

  • Python
  • Java
  • Spring Boot
  • Dark Mode
Home / Spring MVC / XmlViewResolver

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?

Let us know if you liked the post. That’s the only way we can improve.

Share this:

  • Twitter
  • Facebook
  • LinkedIn
  • Reddit

About Lokesh Gupta

A family guy with fun loving nature. Love computers, programming and solving everyday problems. Find me on Facebook and Twitter.

Feedback, Discussion and Comments

  1. Fabio

    January 5, 2017

    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. Leang Socheat

    December 8, 2015

    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. Girish

    February 10, 2015

    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:

    • Lokesh Gupta

      February 10, 2015

      In comment box, please put your code inside [java] … [/java] OR [xml] … [/xml] tags otherwise it may not appear as intended.

Comments are closed on this article!

Search Tutorials

Spring MVC Tutorial

  • MVC – Introduction
  • MVC – Hello World
  • MVC – JSTL
  • MVC – @RequestMapping
  • MVC – Custom Validator
  • MVC – JSR-303 Validation
  • MVC – Dropdown
  • MVC – Submit Form
  • MVC – MessageSourceAware
  • MVC – XmlViewResolver
  • MVC – i18n and i10n
  • MVC – Interceptor
  • MVC – HandlerInterceptor
  • MVC – Multi File Upload (Ajax)
  • MVC – Multi File Upload
  • MVC – File Download
  • MVC – Interview Questions
  • InternalResourceViewResolver
  • ResourceBundleViewResolver
  • SimpleMappingExceptionResolver
  • annotation-config vs component-scan
  • ContextLoaderListener vs DispatcherServlet

Meta Links

  • About Me
  • Contact Us
  • Privacy policy
  • Advertise
  • Guest and Sponsored Posts

Recommended Reading

  • 10 Life Lessons
  • Secure Hash Algorithms
  • How Web Servers work?
  • How Java I/O Works Internally?
  • Best Way to Learn Java
  • Java Best Practices Guide
  • Microservices Tutorial
  • REST API Tutorial
  • How to Start New Blog

Copyright © 2020 · HowToDoInjava.com · All Rights Reserved. | Sitemap

  • Java 15 New Features
  • Sealed Classes and Interfaces
  • EdDSA (Ed25519 / Ed448)