View controllers are used to forward a request to the views without any controllers written either using ViewControllerRegistry or <mvc:view-controller> element. Normally in applications view-controllers are used in static cases when there is no Controller Logic (like getting data for dropdowns) to execute before the view generates the response.
Ways to handle Spring MVC view controller
ViewControllerRegistry
ViewControllerRegistry is a registry for all the view controllers. We do not need to create a Controller class to handle requests, if we are using the
ViewControllerRegistry.
You can register a view into the ViewControllerRegistry as shown below.
1 2 3 4 |
@Override public void addViewControllers(ViewControllerRegistry registry) { registry.addViewController("/").setViewName("home"); } |
<mvc:view-controller> element
We can perform the same functionality as ViewControllerRegistry using the <mvc:view-controller> element. It has 2 attributes path and view-name.
1 |
<mvc:view-controller path="/" view-name="home"/> |
Steps to Configure View Controller
1. Configuration
We have added 2 view controllers in the config file. We have also added the InternalResourceViewResolver to resolver the jsp files.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 |
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:context="http://www.springframework.org/schema/context" 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.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd"> <context:component-scan base-package="com.kscodes.sampleproject" /> <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="prefix"> <value>/WEB-INF/jsp/</value> </property> <property name="suffix"> <value>.jsp</value> </property> </bean> <mvc:view-controller path="/" view-name="home"/> <mvc:view-controller path="/welcome" view-name="welcome"/> </beans> |
2. home and welcome.jsp
Create 2 simple jsp pages.
home.jsp
1 2 3 4 5 6 7 8 |
<html> <body> <h2>This is My Home Page</h2> <h3>Thanks for visiting my home page !!!!!!!!! </h3> </body> </html> |
welcome.jsp
1 2 3 4 5 6 7 8 |
<html> <body> <h2>Welcome Page</h2> <h3>Welcome to kscodes.com </h3> </body> </html> |
Output
When a URL is requested, it first goes to the view controllers, if it finds a matching path in the view controller, then the JSP resolver (InternalResourceViewResolver) tries to find the matching jsp for the view-name.
When we use http://localhost:8080/sampleproject we will see the home.jsp
When we use http://localhost:8080/sampleproject/welcome we will see the welcome.jsp
But note that, you we do not have any view controller nor we have any Controller class for the url http://localhost:8080/sampleproject/home we will get an error.