Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

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

Bitbucket is for the code that takes us to Mars, decodes the human genome, or drives your next car. What will your code do? Get started with Bitbucket today, it's free.

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…

@Override
public void addViewControllers(ViewControllerRegistry registry){
registry.addViewController("/").setViewName("index");
}

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"
 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 xmlns:mvc="http://www.springframework.org/schema/mvc"
 xsi:schemaLocation="
 http://www.springframework.org/schema/beans
 http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
 http://www.springframework.org/schema/mvc
 http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd">
<mvc:view-controller path="/"view-name="welcome"/>
<bean id="viewResolver"
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix"value="/WEB-INF/views/"/>
<property name="suffix"value=".jsp"/>
</bean>
</beans>

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;

@Configuration
@EnableWebMvc
@ComponentScan(basePackages="org.techzoo.springmvc.controller")
public class WebConfig extends WebMvcConfigurerAdapter {

@Override
public void addViewControllers(ViewControllerRegistry registry) {
registry.addViewController("/").setViewName("index");
}

@Bean
public InternalResourceViewResolver viewResolver() {
InternalResourceViewResolver resolver = new InternalResourceViewResolver();
resolver.setPrefix("/WEB-INF/views/");
resolver.setSuffix(".jsp");
return resolver;
}

}

Bitbucket is the Git solution for professional teams who code with a purpose, not just as a hobby. Get started today, it's free.

Topics:
java ,enterprise-integration ,frameworks ,tips and tricks

Published at DZone with permission of Tousif Khan, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

THE DZONE NEWSLETTER

Dev Resources & Solutions Straight to Your Inbox

Thanks for subscribing!

Awesome! Check your inbox to verify your email so you can start receiving the latest in tech news and resources.

X

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

{{ parent.tldr }}

{{ parent.urlSource.name }}