Over a million developers have joined DZone.

Spring MVC 3 View Controller Example

DZone's Guide to

Spring MVC 3 View Controller Example

Spring 3 comes with view-controller which we can use as a tag in XML.

· Java Zone ·
Free Resource

Build vs Buy a Data Quality Solution: Which is Best for You? Gain insights on a hybrid approach. Download white paper now!

When I was writing post on SpringMVC-Tiles3 integration, I used one controller method index() to just return to index.jsp view. It was very stupid way to write a method to just re-direct it to a view if you don’t have anything to do in controller.

Fortunately Spring 3 has a better way where you can configure (or register if you are using XML free configuration) if you don’t have anything to do in controller method and just want to invoke view.

Spring 3 comes with view-controller which we can use as a tag in XML.

<!--Forwards requests tothe"/"resource tothe"index"view-->
<mvc:view-controller path="/"view-name="index"/>

Or alternatively in XML free configuration approach…

public void addViewControllers(ViewControllerRegistry registry){

Behind the scenes, mvc:view-controller registers a ParameterizableViewController that selects a view for rendering. In this case, when “/” is requested, the index view is rendered. The actual view template is a .jsp resolved inside the /WEB-INF/views directory.

The complete XML file will look similar to below.

<?xml version="1.0"encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
<mvc:view-controller path="/"view-name="welcome"/>
<bean id="viewResolver"
<property name="prefix"value="/WEB-INF/views/"/>
<property name="suffix"value=".jsp"/>

XML Free Web MVC Configuration will need following implementation.

package or.techzoo.springmvc;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.config.annotation.ViewControllerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
import org.springframework.web.servlet.view.InternalResourceViewResolver;

public class WebConfig extends WebMvcConfigurerAdapter {

public void addViewControllers(ViewControllerRegistry registry) {

public InternalResourceViewResolver viewResolver() {
InternalResourceViewResolver resolver = new InternalResourceViewResolver();
return resolver;


Build vs Buy a Data Quality Solution: Which is Best for You? Maintaining high quality data is essential for operational efficiency, meaningful analytics and good long-term customer relationships. But, when dealing with multiple sources of data, data quality becomes complex, so you need to know when you should build a custom data quality tools effort over canned solutions. Download our whitepaper for more insights into a hybrid approach.

java ,enterprise-integration ,frameworks ,tips and tricks

Published at DZone with permission of

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}