ViewResolvers
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
1. ViewResolver
It will provide mapping between View names and the actual views.
2. View
It will take care of preparing Request and handover 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
1. 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.
E.g: 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.
E.g
<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
2. taglibs-standard-spec-1.2.5.jar
In general, the above jar files are existed in “C:\Tomcat 9.0\webapps\examples\WEB-INF\lib” location.
index.jsp
<%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%> <!DOCTYPE html> <html> <head> <meta charset="ISO-8859-1"> <title>Insert title here</title> </head> <body> <jsp:forward page="helloform"/> </body> </html>
helloform.jsp
<%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%> <!DOCTYPE html PUBLIC "- //W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"> <title>Insert title here</title> </head> <body> <h2 style="color: red;" align="center">Welcome to Waytoeasylearn</h2> <h3 style="color: blue;" align="center">User Hello Form</h3> <form method="POST" action="wish"> <center> <table> <tr> <td>User Name</td> <td><input type="text" name="uname" /></td> </tr> <tr> <td><input type="submit" value="SayHello" /></td> </tr> </table> </center> </form> </body> </html>
wish.jsp
<%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%> <%@page isELIgnored="false"%> <!DOCTYPE html> <html> <head> <meta charset="ISO-8859-1"> <title>Spring MVC</title> </head> <body> <h1>Waytoeasylearn</h1> <h2>Hello..!!, ${message}.</h2> </body> </html>
HelloController.java
package com.ashok.spring.mvc.controller; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.springframework.web.servlet.ModelAndView; import org.springframework.web.servlet.mvc.Controller; public class HelloController implements Controller { @Override public ModelAndView handleRequest(HttpServletRequest request, HttpServletResponse response) throws Exception { return new ModelAndView("helloform"); } }
WishController.java
package com.ashok.spring.mvc.controller; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.springframework.web.servlet.ModelAndView; import org.springframework.web.servlet.mvc.Controller; public class WishController implements Controller { @Override public ModelAndView handleRequest(HttpServletRequest request, HttpServletResponse response) throws Exception { String uname = request.getParameter("uname"); return new ModelAndView("wish", "uname", uname); } }
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"> <context:annotation-config/> <context:component-scan base-package="com.ashok.spring.mvc.controller"/> <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/jsp/"/> <property name="suffix" value=".jsp"/> </bean> </beans>
web.xml
<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd" id="WebApp_ID" version="4.0"> <display-name>MVC</display-name> <welcome-file-list> <welcome-file>index.html</welcome-file> <welcome-file>index.htm</welcome-file> <welcome-file>index.jsp</welcome-file> <welcome-file>default.html</welcome-file> <welcome-file>default.htm</welcome-file> <welcome-file>default.jsp</welcome-file> </welcome-file-list> <servlet> <servlet-name>ds</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>ds</servlet-name> <url-pattern>/</url-pattern> </servlet-mapping> </web-app>
2. 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 “Internal ResourceView” class as “viewClass”.
If we want to use this ViewResolver we must use the following properties.
1. prefix: It will take parent location of the View JSp page, that is, /WEB-INF/
2. 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.
<beans> ---- <bean name="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="prefix" value="/WEB-INF/"/> <property name="suffix" value=".jsp"/> </bean> ----- </beans>
3. 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 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
<?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.
ds-servlet.xml
<beans> ---- <bean name="viewResolver" class="org.springframework.web.servlet.view.XmlViewResolver"> <property name="location" value="/WEB-INF/views.xml"/> ----- </beans>
4. ResourceBundleViewResolver:
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 format 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
E.g
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.
<beans> ---- <bean name="viewResolver" class=" org.springframework.web.servlet.view.ResourceBundleViewResolver"> <property name="basename" value="views"/> </bean> ---- </beans>