SpringMVC ViewController - Spring MVC 3  view controller example

Spring MVC 3 view controller example

Posted on

SpringMVC ViewController - Spring MVC 3  view controller example

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.

Or alternatively in XML free configuration approach…

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 Free Web MVC Configuration will need following implementation.

Source techzoo.org