In Spring MVC based application, the last step of request processing is to return the logical view name. Here DispatcherServlet has to delegate control to a view template so the information is rendered. This view template decides that which view should be rendered based on returned logical view name. These view templates are one or more view resolver beans declared in the web application context. These beans have to implement the
ViewResolver interface for DispatcherServlet to auto-detect them. Spring MVC comes with several
ViewResolver implementations. In this example, we will look at such a view resolver template i.e. InternalResourceViewResolver.
In most of the applications, views are mapped to a template’s name and location directly.
InternalResourceViewResolver helps in mapping the logical view names to directly view files under a certain pre-configured directory. To register
InternalResourceViewResolver, you can declare a bean of this type in the web application context.
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="viewClass" value="org.springframework.web.servlet.view.JstlView" /> <property name="prefix" value="/WEB-INF/jsp/" /> <property name="suffix" value=".jsp" /> </bean>
After above configuration, InternalResourceViewResolver will resolves the view names ‘home’ and ‘admin/home’ etc. in the following way.
|Logical View Name||Actual View File|
InternalResourceViewResolver resolves view names into view objects of type
JstlView if the JSTL library (i.e., jstl.jar) is present in the classpath. So, you can omit the viewClass property if your views are JSP templates with JSTL tags. Otherwise you will need to provide a matching viewClass i.e.
org.springframework.web.servlet.view.tiles2.TilesView if your view is based on tiles.
Drop me your queries if something needs more explanation.
Happy Learning !!