Home  >  Spring MVC

Spring MVC XmlViewResolver Example with JstlView and RedirectView

By Arvind Rai, April 04, 2015
In this page we will provide Spring MVC XmlViewResolver example with JstlView and RedirectView. XmlViewResolver handles views defined in XML. This helps to achieve view definition in a separate XML file that we can put at our required location. We need to refer this location in XmlViewResolver using setLocation() method. In our example, we using java configuration file to create the bean of XmlViewResolver . Find the complete example.

Project Structure in Eclipse

Find the project structure in eclipse.
Spring MVC  XmlViewResolver  Example with JstlView and RedirectView

Create XML View with JstlView and RedirectView

We need to create an XML file at our required location and define the URL using JstlView and RedirectView.

JstlView : The view which uses JSP standard tag library (JSTL).
RedirectView : The view that redirects the absolute or context relative URL.

Find the XML view used in our demo.
<beans xmlns="http://www.springframework.org/schema/beans"

    <bean id="success" class="org.springframework.web.servlet.view.JstlView">
        <property name="url" value="/views/success.jsp" />
    <bean id="home" class="org.springframework.web.servlet.view.RedirectView">
        <property name="url" value="http://concretepage.com" />
We have created here a XML view for user. There are two bean definitions, one for JstlView and second for RedirectView . When the controller method returns the bean name as string, then corresponding URL is invoked. Suppose that controller method return success value, the corresponding bean will be invoked and the URL /views/success.jsp will respond. In the same way for home value, the absolute URL http://concretepage.com will open. According to our requirement, we can use one or more combination of JstlView and RedirectView in our XML view.

Java Configuration for XmlViewResolver

XmlViewResolver uses an XML file as view definition in which we define inner and external URLs with the help of JstlView and RedirectView. Default XML file location is "/WEB-INF/views.xml". But we can assign any location. To configure view location, XmlViewResolver provides XmlViewResolver.setLocation() method. Find the definition of XmlViewResolver.
package com.concretepage.config;  
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;
import org.springframework.web.servlet.view.XmlViewResolver;
public class AppConfig {  
    public XmlViewResolver xmlViewResolver() {  
	XmlViewResolver resolver = new XmlViewResolver();
	Resource resource = new ClassPathResource("user/user-view.xml");  
        return resolver;  

Create Controller

Create a controller class.
package com.concretepage;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
public class WelcomeController {
        public String hello(Model model) {
	    model.addAttribute("msg", "XmlViewResolver Demo");
            return "success";
        public String getInfo() {
            return "home";

Create JSP

Find the JSP used in our demo.
<title>Spring MVC</title>


To test the application, download code and run gradle as gradle clean build. Deploy war file in tomcat.
1. Find the URL to test JstlView.
Spring MVC  XmlViewResolver  Example with JstlView and RedirectView
2. Find the URL to test RedirectView.
As an output http://www.concretepage.com/ will open.

Download Complete Source Code


©2019 concretepage.com | Privacy Policy | Contact Us