How to Resolve View
In Spring MVC, InternalResourceViewResolver allow us to add some predefined prefix and suffix to the view name (prefix + view name + suffix), and generate the final view page URL as below.
prefix + logical view name + suffix = /WEB-INF/view/MyPage.jsp
Configuring InternalResourceViewResolver in Spring MVC
To configure InternalResourceViewResolver, you can declare a bean of this type in the web application context either with XML configuration or with Java Configuration as below.
In XML Configuration
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="suffix" value=".jsp"/> <property name="prefix" value="/WEB-INF/view/"/> </bean>
In Java Configuration
@Bean public ViewResolver viewResolver(){ InternalResourceViewResolver viewResolver = new InternalResourceViewResolver(); viewResolver.setPrefix("/WEB-INF/view/"); viewResolver.setSuffix(".jsp"); return viewResolver; }
InternalResourceViewResolver resolves view names into view objects of type JstlView by default if the JSTL library jstl.jar available into classpath of the application. So you do not need to explicitly add the viewClass property. But you can override this property by adding viewClass property of InternalResourceViewResolver. In below code we add other view class TilesView instead of JstlView class based on tiles.
In XML Configuration
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="suffix" value=".jsp"/> <property name="prefix" value="/WEB-INF/view/"/> <property name="viewClass" value="org.springframework.web.servlet.view.tiles2.TilesView" /> </bean>
In Java Configuration
@Bean public ViewResolver viewResolver(){ InternalResourceViewResolver viewResolver = new InternalResourceViewResolver(); viewResolver.setPrefix("/WEB-INF/view/"); viewResolver.setSuffix(".jsp"); viewResolver.setViewClass(TilesView.class); return viewResolver; }
What’s internal resource views?
In Spring MVC or any web application, for good practice, it’s always recommended to put the entire views or JSP files under “WEB-INF” folder, to protect it from direct access via manual entered URL. Those views under “WEB-INF” folder are named as internal resource views, as it’s only accessible by the servlet or Spring’s controllers class.
Summary
- Controller return the logical view name to the DispatcherServlet.
- 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.
- Spring MVC Web Tutorial
- Spring MVC Interview Questions
- MVC Design Pattern
- Spring MVC DispatcherServlet
- Spring MVC WebApplicationContext and Root Application Context
- Spring MVC @Controller Annotation
- Spring MVC @RequestMapping Annotation
- Spring MVC @RequestParam Annotation
- Spring MVC ContextLoaderListener
- Spring MVC @RequestParam and @PathVariable annotations
- Spring MVC Hello World Example
- Spring MVC Exception Handling Example
- Spring MVC with Hibernate CRUD Example
- Spring MVC Tiles Plugin with Example
- Spring MVC Interceptor with example
- Spring MVC with MongoDB CRUD Example
- Spring MVC Internationalization & Localization with Example