InternalResourceViewResolver Example

This is an example program that teaches you how you can use the InternalResourceViewResolver in your Spring Framework.

InternalResourceViewResolver Example

InternalResourceViewResolver Example

This tutorial show you how to correctly use the InternalResourceViewResolver in your web applications.

We have created the example code that shows you how to use the InternalResourceViewResolver. You can also download the source code of the example discussed here.

     

InternalResourceViewResolver example in Spring 2.5 MVC

In this tutorial we are discussing about InternalResourceViewResolver class, which is one of the several view resolvers in Spring 2.5.

Spring 2.5 have two most important way for handles views. First is ViewResolver that provides mapping between view names and real views. Second is View interface that provides the view for the requests. In this tutorial we will discuss about InternalResourceViewResolver that support Servlet, JSP and subclasses like JstlView and TilesView.

Step 1:

We will create a index.jsp file inside the WebContent folder. The index.jsp will have a link that generate user request. The code of the index.jsp is:

<%@page contentType="text/html" pageEncoding="UTF-8"%>

<html>

<head>

<title>Internal Resource View Resolver</title>

</head>

<body>

<center>

<a href="viewresolver.html">Internal Resource View Resolver</a>

</center>

</body>

</html>

Step 2:

Now we will configure the web.xml file for DispatcherServlet. The code of the web.xml is:

<?xml version="1.0" encoding="UTF-8"?>

<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

xmlns="http://java.sun.com/xml/ns/j2ee"

xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"

xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_5.xsd"

version="2.5">

<servlet>

<servlet-name>dispatcher</servlet-name>

<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> 0

<load-on-startup>1</load-on-startup>

</servlet>

<servlet-mapping> 1

<servlet-name>dispatcher</servlet-name>

<url-pattern>*.html</url-pattern>

</servlet-mapping> 2

<welcome-file-list>

<welcome-file>index.jsp</welcome-file>

</welcome-file-list> 3

<taglib>

<taglib-uri>/spring</taglib-uri>

<taglib-location>/WEB-INF/spring.tld</taglib-location> 4

</taglib>

</web-app>

Step 3: 5

Now we will create a dispatcher-servlet.xml file inside the /WEB-INF/ folder. The dispatcher-servlet.xml file will contain code for the urlMapping and viewResolver. The code for include InternalResourceViewResolver as a resolver like:

<bean id="viewResolver"
class="org.springframework.web.servlet.view.InternalResourceViewResolver" >
<
property name="prefix">
<
value>/WEB-INF/jsp/</value>
</
property>
<
property name="suffix">
<
value>.jsp</value>
</
property>
</
bean

Spring MVC controllers are always return ModelAndView after that InternalResourceViewResolver resolver that and add prefix and suffix and provide the view for the particular request. The full code of the dispatcher-servlet.xml is: 6

<?xml version="1.0" encoding="UTF-8"?>

<beans xmlns="http://www.springframework.org/schema/beans"

xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 7

xsi:schemaLocation="http://www.springframework.org/schema/beans

http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">

<bean class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping"> 8

<property name="mappings">

<props>

<prop key="/viewresolver.html">viewResolverController</prop> 9

</props>

</property>

</bean> 0

<bean name="viewResolverController"

class="org.springframework.web.servlet.mvc.ParameterizableViewController">

<property name="viewName" value="message" /> 1

</bean>

<bean id="viewResolver"

class="org.springframework.web.servlet.view.InternalResourceViewResolver" > 2

<property name="prefix">

<value>/WEB-INF/jsp/</value>

</property> 3

<property name="suffix">

<value>.jsp</value>

</property> 4

</bean>

</beans>

Step 4: 5

Now we will create a message.jsp file inside the /WEB-INF/jsp/ folder that display a message for the user as response. The code of the message.jsp is:

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"

pageEncoding="ISO-8859-1"%> 6

<html>

<head>

<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"> 7

<title>Internal Resource View Resolver Example</title>

</head>

<body> 8

<center>

<table>

<tr> 9

<td>

<b>This is Internal Resource View Resolver Example.</b>

</td> 0

</tr>

</table>

</center> 1

</body>

</html>

Step 5: 2

Now we will run this example and see the output like:

And after click on this link, the output like: 3

Download code

Download this example code  4