Nearby lessons

27 of 35

Spring - ViewResolvers

📌 What You Will Learn
  • 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:

Example03
JCode Cell
1 
2<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
3pageEncoding="ISO-8859-1"%>
4<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
5<html>
6<head>
7<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
8<title>Insert title here</title>
9</head>
10<body>
11<jsp:forward page="hello"/>
12</body>
13</html>
14

taglibs-standard-spec-1.2.5.jar — helloform.jsp

Example04
JCode Cell
1 
2<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
3pageEncoding="ISO-8859-1"%>
4<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
5<html>
6<head>
7<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
8<title>Insert title here</title>
9</head>
10<body>
11<h2 style="color: Red;" align="center">Durga Software Solutions</h2>
12<h3 style="color: Blue;" align="center">User Hello Page</h3>
13<form method="POST" action="hello">
14<center>
15<table>
16<tr>
17<td>User Name</td>
18<td><input type="text" name="uname"/></td>
19</tr>
20<tr>
21<td><input type="submit" value="SayHello"/></td>
22</tr>
23</table>
24</center>
25</form>
26</body>
27</html>
28

taglibs-standard-spec-1.2.5.jar — wish.jsp

Example05
JCode Cell
1 
2<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
3pageEncoding="ISO-8859-1"%>
4<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
5<html>
6<head>
7<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
8<title>Insert title here</title>
9</head>
10<body>
11<h2 style="color: Red;" align="center">Durga Software Solutions</h2>
12<h3 style="color: Blue;" align="center">User Wish Page</h3>
13<h1 style="color: Red;" align="center">Hello ${uname}</h1>
14</body>
15</html>
16

taglibs-standard-spec-1.2.5.jar — HelloController.java

Example06
JCode Cell
1 
2package com.durgasoft.controller;
3 
4import javax.servlet.http.HttpServletRequest;
5import javax.servlet.http.HttpServletResponse;
6 
7import org.springframework.stereotype.Controller;
8import org.springframework.web.bind.annotation.RequestMapping;
9import org.springframework.web.bind.annotation.RequestMethod;
10 
11import org.springframework.web.servlet.ModelAndView;
12 
13 
14@Controller
15@RequestMapping("/hello")
16public class HelloController {
17@RequestMapping(method=RequestMethod.GET)
18public String showForm() {
19return "helloform";
20}
21 
22@RequestMapping(method=RequestMethod.POST)
23public ModelAndView wish(HttpServletRequest request, HttpServletResponse response){
24String uname = request.getParameter("uname");
25return new ModelAndView("wish", "uname", uname);
26}
27}
28

taglibs-standard-spec-1.2.5.jar — ds-servlet.xml

Example07
JCode Cell
1 
2<?xml version="1.0" encoding="UTF-8"?>
3<beans xmlns="http://www.springframework.org/schema/beans"
4xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
5xmlns:p="http://www.springframework.org/schema/p"
6xmlns:context="http://www.springframework.org/schema/context"
7xsi:schemaLocation="
8 http://www.springframework.org/schema/beans
9 http://www.springframework.org/schema/beans/spring-beans.xsd
10 http://www.springframework.org/schema/context
11http://www.springframework.org/schema/context/spring-context.xsd">
12<context:annotation-config/>
13<context:component-scan base-package="com.durgasoft.controller"/>
14 
15<bean name="viewResolver" class="org.springframework.web.servlet.view.UrlBasedViewResolver">
16<property name="viewClass" value="org.springframework.web.servlet.view.JstlView"/>
17<property name="prefix" value="/WEB-INF/pages"/>
18<property name="suffix" value=".jsp"/>
19</bean>
20</beans>
21

taglibs-standard-spec-1.2.5.jar — web.xml

Example08
JCode Cell
1 
2<?xml version="1.0" encoding="UTF-8"?>
3<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">
4<display-name>app19</display-name>
5<welcome-file-list>
6<welcome-file>index.html</welcome-file>
7<welcome-file>index.htm</welcome-file>
8<welcome-file>index.jsp</welcome-file>
9<welcome-file>default.html</welcome-file>
10<welcome-file>default.htm</welcome-file>
11<welcome-file>default.jsp</welcome-file>
12</welcome-file-list>
13 
14<servlet>
15<servlet-name>ds</servlet-name>
16<servlet-class> org.springframework.web.servlet.DispatcherServlet</servlet-class>
17<load-on-startup>1</load-on-startup>
18</servlet>
19<servlet-mapping>
20<servlet-name>ds</servlet-name>
21<url-pattern>/</url-pattern>
22</servlet-mapping>
23</web-app>
24

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:

Example11
JCode Cell
1 
2<beans>
3----
4<bean name="viewResolver" class="org.springframework.web.servlet.view.XmlViewResolver">
5 
6 <property name="location" value="/WEB-INF/views.xml"/>
7-----
8</beans>
9

WEB-INF/views.xml — index.jsp

Example:

Example12
JCode Cell
1 
2<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
3pageEncoding="ISO-8859-1"%>
4<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
5<html>
6<head>
7<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
8<title>Insert title here</title>
9</head>
10<body>
11<jsp:forward page="hello"/>
12</body>
13</html>
14

WEB-INF/views.xml — helloform.jsp

Example13
JCode Cell
1 
2<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
3pageEncoding="ISO-8859-1"%>
4<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
5<html>
6<head>
7<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
8<title>Insert title here</title>
9</head>
10<body>
11<h2 style="color: Red;" align="center">Durga Software Solutions</h2>
12<h3 style="color: Blue;" align="center">User Hello Page</h3>
13<form method="POST" action="hello">
14<center>
15<table>
16<tr>
17<td>User Name</td>
18<td><input type="text" name="uname"/></td>
19</tr>
20<tr>
21<td><input type="submit" value="SayHello"/></td>
22</tr>
23</table>
24</center>
25</form>
26</body>
27</html>
28

WEB-INF/views.xml — wish.jsp

Example14
JCode Cell
1 
2<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
3pageEncoding="ISO-8859-1"%>
4<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
5<html>
6<head>
7<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
8<title>Insert title here</title>
9</head>
10<body>
11<h2 style="color: Red;" align="center">Durga Software Solutions</h2>
12<h3 style="color: Blue;" align="center">User Wish Page</h3>
13<h1 style="color: Red;" align="center">Hello ${uname}</h1>
14</body>
15</html>
16

WEB-INF/views.xml — HelloController.java

Example15
JCode Cell
1 
2package com.durgasoft.controller;
3 
4import javax.servlet.http.HttpServletRequest;
5import javax.servlet.http.HttpServletResponse;
6 
7import org.springframework.stereotype.Controller;
8import org.springframework.web.bind.annotation.RequestMapping;
9import org.springframework.web.bind.annotation.RequestMethod;
10 
11import org.springframework.web.servlet.ModelAndView;
12 
13 
14@Controller
15@RequestMapping("/hello")
16public class HelloController {
17@RequestMapping(method=RequestMethod.GET)
18public String showForm() {
19return "helloform";
20}
21 
22@RequestMapping(method=RequestMethod.POST)
23public ModelAndView wish(HttpServletRequest request, HttpServletResponse response){
24String uname = request.getParameter("uname");
25return new ModelAndView("wish", "uname", uname);
26}
27}
28

WEB-INF/views.xml — views.xml

Example16
JCode Cell
1 
2<?xml version="1.0" encoding="UTF-8"?>
3<beans xmlns="http://www.springframework.org/schema/beans"
4xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
5xmlns:p="http://www.springframework.org/schema/p"
6xmlns:context="http://www.springframework.org/schema/context"
7xsi:schemaLocation="
8 http://www.springframework.org/schema/beans
9 http://www.springframework.org/schema/beans/spring-beans.xsd
10 http://www.springframework.org/schema/context
11http://www.springframework.org/schema/context/spring-context.xsd">
12<bean name="helloform" class="org.springframework.web.servlet.view.JstlView">
13<property name="url" value="/WEB-INF/pages/helloform.jsp"/>
14</bean>
15<bean name="wish" class="org.springframework.web.servlet.view.JstlView">
16<property name="url" value="/WEB-INF/pages/wish.jsp"/>
17</bean>
18</beans>
19

WEB-INF/views.xml — ds-servlet.xml

Example17
JCode Cell
1 
2<?xml version="1.0" encoding="UTF-8"?>
3<beans xmlns="http://www.springframework.org/schema/beans"
4xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
5xmlns:p="http://www.springframework.org/schema/p"
6xmlns:context="http://www.springframework.org/schema/context"
7xsi:schemaLocation="
8 http://www.springframework.org/schema/beans
9 http://www.springframework.org/schema/beans/spring-beans.xsd
10 http://www.springframework.org/schema/context
11http://www.springframework.org/schema/context/spring-context.xsd">
12<context:annotation-config/>
13<context:component-scan base-package="com.durgasoft.controller"/>
14 
15<bean name="handlerMapping" class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping"/>
16<bean name="viewResolver" class="org.springframework.web.servlet.view.XmlViewResolver">
17<property name="location" value="/WEB-INF/views.xml"/>
18</bean>
19</beans>
20

WEB-INF/views.xml — web.xml

Example18
JCode Cell
1 
2<?xml version="1.0" encoding="UTF-8"?>
3<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">
4<display-name>app19</display-name>
5<welcome-file-list>
6<welcome-file>index.html</welcome-file>
7<welcome-file>index.htm</welcome-file>
8<welcome-file>index.jsp</welcome-file>
9<welcome-file>default.html</welcome-file>
10<welcome-file>default.htm</welcome-file>
11<welcome-file>default.jsp</welcome-file>
12</welcome-file-list>
13 
14<servlet>
15<servlet-name>ds</servlet-name>
16<servlet-class> org.springframework.web.servlet.DispatcherServlet</servlet-class>
17<load-on-startup>1</load-on-startup>
18</servlet>
19<servlet-mapping>
20<servlet-name>ds</servlet-name>
21<url-pattern>/</url-pattern>
22</servlet-mapping>
23 </web-app>
24

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:

Example19
JCode Cell
1 
2<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
3pageEncoding="ISO-8859-1"%>
4<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
5<html>
6<head>
7<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
8<title>Insert title here</title>
9</head>
10<body>
11<jsp:forward page="hello"/>
12</body>
13</html>
14

ResourceBundleViewResolver — helloform.jsp

Example20
JCode Cell
1 
2<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
3pageEncoding="ISO-8859-1"%>
4<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
5<html>
6<head>
7<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
8<title>Insert title here</title>
9</head>
10<body>
11<h2 style="color: Red;" align="center">Durga Software Solutions</h2>
12<h3 style="color: Blue;" align="center">User Hello Page</h3>
13<form method="POST" action="hello">
14<center>
15<table>
16<tr>
17<td>User Name</td>
18<td><input type="text" name="uname"/></td>
19</tr>
20<tr>
21<td><input type="submit" value="SayHello"/></td>
22</tr>
23</table>
24</center>
25</form>
26</body>
27</html>
28

ResourceBundleViewResolver — wish.jsp

Example21
JCode Cell
1 
2<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
3pageEncoding="ISO-8859-1"%>
4<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
5<html>
6<head>
7<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
8<title>Insert title here</title>
9</head>
10<body>
11<h2 style="color: Red;" align="center">Durga Software Solutions</h2>
12<h3 style="color: Blue;" align="center">User Wish Page</h3>
13<h1 style="color: Red;" align="center">Hello ${uname}</h1>
14</body>
15</html>
16

ResourceBundleViewResolver — HelloController.java

Example22
JCode Cell
1 
2package com.durgasoft.controller;
3 
4import javax.servlet.http.HttpServletRequest;
5import javax.servlet.http.HttpServletResponse;
6 
7import org.springframework.stereotype.Controller;
8import org.springframework.web.bind.annotation.RequestMapping;
9import org.springframework.web.bind.annotation.RequestMethod;
10 
11import org.springframework.web.servlet.ModelAndView;
12 
13@Controller
14@RequestMapping("/hello")
15public class HelloController {
16@RequestMapping(method=RequestMethod.GET)
17public String showForm() {
18 
19return "helloform";
20}
21@RequestMapping(method=RequestMethod.POST)
22public ModelAndView wish(HttpServletRequest request, HttpServletResponse response){
23String uname = request.getParameter("uname");
24return new ModelAndView("wish", "uname", uname);
25}
26}
27

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

Example23
JCode Cell
1 
2<?xml version="1.0" encoding="UTF-8"?>
3<beans xmlns="http://www.springframework.org/schema/beans"
4xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
5xmlns:p="http://www.springframework.org/schema/p"
6xmlns:context="http://www.springframework.org/schema/context"
7xsi:schemaLocation="
8 http://www.springframework.org/schema/beans
9 http://www.springframework.org/schema/beans/spring-beans.xsd
10 http://www.springframework.org/schema/context
11http://www.springframework.org/schema/context/spring-context.xsd">
12<context:annotation-config/>
13<context:component-scan base-package="com.durgasoft.controller"/>
14<bean name="viewResolver" class=" org.springframework.web.servlet.view.ResourceBundleViewResolver">
15<property name="basename" value="views"/>
16</bean>
17</beans>
18

views.properties — web.xml

Example24
JCode Cell
1 
2<?xml version="1.0" encoding="UTF-8"?>
3<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">
4<display-name>app19</display-name>
5<welcome-file-list>
6<welcome-file>index.html</welcome-file>
7<welcome-file>index.htm</welcome-file>
8<welcome-file>index.jsp</welcome-file>
9<welcome-file>default.html</welcome-file>
10<welcome-file>default.htm</welcome-file>
11<welcome-file>default.jsp</welcome-file>
12</welcome-file-list>
13 
14<servlet>
15<servlet-name>ds</servlet-name>
16<servlet-class> org.springframework.web.servlet.DispatcherServlet</servlet-class>
17<load-on-startup>1</load-on-startup>
18</servlet>
19<servlet-mapping>
20<servlet-name>ds</servlet-name>
21<url-pattern>/</url-pattern>
22</servlet-mapping>
23</web-app>
24
📝 Key Takeaways
  • 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