Nearby lessons
27 of 35Spring - ViewResolvers
- Understand why a controller returns a view name rather than a file path
- Configure prefix/suffix resolution with UrlBasedViewResolver and InternalResourceViewResolver
- Declare views in views.xml and in views.properties, and know when that is worth it
How a controller's logical view name becomes an actual page. Four resolvers with configuration and examples: UrlBasedViewResolver, InternalResourceViewResolver, XmlViewResolver and ResourceBundleViewResolver.
ViewResolvers In Spring WEB MVC
In Spring MVC, the main intention of view resolvers are to enable us to render models in a browser without tying you to a specific view technology like JSP, Velocity, XML…etc.
To handle views, Spring has provided the following two interfaces
- ViewResolver
- View
- ViewResolver: It will provide mapping between View names and the actual views.
- View: It will take care of preparing Request and handsover to the respective View Technology.
In Spring WEB MVC applications, ViewResolver takes logical name provided by Controller class through DispatcherServlet and return View object point to the actual View page, in this context, DispatcherServlet will access render() method on View object to render the control to view resource.
Spring Web MVC has provided the following ViewResolvers to handle View in Spring WEB MVC applications.
- AbstractCachingViewResolver
- XmlViewResolver
- ResourceBundleViewResolver
- UrlBasedViewResolver
- InternalResourceViewResolver
- VelocityViewResolver/FreeMarkerViewResolver
From the above list of View Resolovers , mainly we are able to use the following ViewResolvers in Spring WEB MVC Applications
- UrlBasedViewResolver
- InternalResourceViewResolver
- XmlViewResolver
- ResourceBundleViewResolver
UrlBasedViewResolver
This ViewResolver will take view logical name from Controller through DispatcherServlet and searches for the view resources whose name is same as logical view name.
This ViewResolver must required the following three properties configuration.
- viewClass: It will take a particular View Class inorder to render response.
EX: org.springframework.web.servlet.view.JstlView.
- prefix: It will take the location of the View pages under WEB-INF.
- suffix: It will take the View page extension.
EX:
<beans> <bean name="viewResolver"
class="org.springframework.web.servlet.view.UrlBasedViewResolver"> <property name="viewClass" value="org.springframework.web.servlet.view.JstlView"/> <property name="prefix" value="/WEB-INF/pages/"/> <property name="suffix" value=".jsp"/>
</bean> </beans>
Note: If we want to use JstlView class as value to the property "viewClass" then we must provide the following jar files in web application "lib" folder.
1 .taglibs-standard-impl-1.2.5.jar
taglibs-standard-spec-1.2.5.jar — index.jsp
In general, the above jar files are existed in "C:\Tomcat 9.0\webapps\examples\WEB-INF\lib" location.
Example:
taglibs-standard-spec-1.2.5.jar — helloform.jsp
taglibs-standard-spec-1.2.5.jar — wish.jsp
taglibs-standard-spec-1.2.5.jar — HelloController.java
taglibs-standard-spec-1.2.5.jar — ds-servlet.xml
taglibs-standard-spec-1.2.5.jar — web.xml
InternalResourceViewResolver
InternalResourceViewResolver is sub class to UrlBasedViewResolver, it maps jsp and html files which are existed in the WebContent/WEB-INF/ folder.
This ViewResolver will take JstlView as default view, if we want to use Jsp pages as view pages with out Jstl tags then we have to use "InternalResourceView" class as "viewClass".
If we want to use this ViewResolver we must use the following properties.
- prefix: It will take parent location of the View JSp page, that is, /WEB-INF/
- suffix: It will take view page extension like .jsp or .html
To configure this ViewResolver in Spring Configuration File we have to use the following XML tags.
EX:
<beans> ---- <bean name="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/"/> <property name="suffix" value=".jsp"/> </bean> ----- </beans>
XmlViewResolver
XmlViewResolver is used to resolve the view names using view beans defined in xml file.
If we want to use this ViewResolver in Spring WEB MVC applications then we have to prepare an XML file with all View Pages configuration by using <bean> tags. In Views XML configuration file we have to configure "JstlView" class as bean with the logical name which is returned by the Controller class, in side JstlView class configuration we have to provide a property "url" with the name and location of the respective View page.
Note: The default name of the Xml file is "view.xml" file.
WEB-INF/views.xml — ds-servlet.xml
- <?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:p="http://www.springframework.org/schema/p"
- xmlns:context="http://www.springframework.org/schema/context"
- xsi:schemaLocation="
- http://www.springframework.org/schema/beans
- http://www.springframework.org/schema/beans/spring-beans.xsd
- http://www.springframework.org/schema/context
- http://www.springframework.org/schema/context/spring-context.xsd">
- <bean name="helloform" class="org.springframework.web.servlet.view.JstlView">
- <property name="url" value="/WEB-INF/pages/helloform.jsp"/>
- </bean>
- <bean name="wish" class="org.springframework.web.servlet.view.JstlView">
- <property name="url" value="/WEB-INF/pages/wish.jsp"/>
- </bean>
- </beans>
After providing View configuration XML file then we must configure View Configuration file in Spring Configuration File.
To provide View Configuration File configuration in Spring Configuration file we have to use "location" property with the View configuration XMl file location in "XmlViewResolver" configuration.
EX:
WEB-INF/views.xml — index.jsp
Example:
WEB-INF/views.xml — helloform.jsp
WEB-INF/views.xml — wish.jsp
WEB-INF/views.xml — HelloController.java
WEB-INF/views.xml — views.xml
WEB-INF/views.xml — ds-servlet.xml
WEB-INF/views.xml — web.xml
ResourceBundleViewResolver — index.jsp
ResourceBundleViewResolver is used to resolve the view names using properties file.
If we want to use this ViewResolver in Spring WEB MVC applications then we have to prepare a properties file under "src" folder with all View Pages configuration by using key-value pairs.
In Views properties file we have to configure "JstlView" class as value with the logical name which is returned by the Controller class in the formate like
logical_Name.(class) = org.springframework.web.servlet.view.JstlView
In Views properties file, we have to provide a key "url" with the name and location of the respective View page with the following format. logical_Name.url = /WEB-INF/pages/file_Name.jsp
EX: src/views.properties
helloform.(class)=org.springframework.web.servlet.view.JstlView helloform.url=/WEB-INF/pages/helloform.jsp
After preparing views properties file, we have to configure views properties file in Spring configuration file under "ResourceBundleViewResolver" with the "basename" property.
EX:
<beans> ---- <bean name="viewResolver"
class=" org.springframework.web.servlet.view.ResourceBundleViewResolver"> <property name="basename" value="views"/> </bean> ---- </beans>
Example:
ResourceBundleViewResolver — helloform.jsp
ResourceBundleViewResolver — wish.jsp
ResourceBundleViewResolver — HelloController.java
views.properties — ds-servlet.xml
helloform.(class)=org.springframework.web.servlet.view.JstlView helloform.url=/WEB-INF/pages/helloform.jsp wish.(class)=org.springframework.web.servlet.view.JstlView wish.url=/WEB-INF/pages/wish.jsp
views.properties — web.xml
- The resolver is the seam that keeps controllers free of JSP paths
- Prefix + suffix resolution covers most applications with no per-view configuration
- XmlViewResolver and ResourceBundleViewResolver pay off when views need their own settings