Latest Tutorials| Questions and Answers|Ask Questions?|Site Map



Home Spring UrlBasedViewResolver Example

 
 

Share on Google+Share on Google+

UrlBasedViewResolver Example

Advertisement
UrlBasedViewResolver Example in Spring. Learn how to use UrlBasedViewResolver in Spring web applications. You can learn the use of URL Based View Resolver with the help of example code discussed here. You download and test the example code of UrlBasedViewResolver.

UrlBasedViewResolver Example in Spring 2.5 MVC

     

The UrlBasedViewResolver is provides the mapping logical view names and URLs directly that hands over to the view class specified. The UrlBasedViewResolver provides a convenient shape called InternalResourceViewResolver that support JSP, Servlet, JstlViews and TileViews. In this example, we will discuss about how to use of UrlBasedViewResolver in your project.

Step 1:

Now we will create a index.jsp file that will have a link to generate user request. The code of the index.jsp is:

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

<html>

<head>

<title>URL Based View Resolver</title>

</head>

<body>

<center>

<a href="urlbasedviewresolver.html">URL Based View Resolver</a>

</center>

</body>

</html>

Step 2:

Now we will customize the the web.xml file for the DispatcherServlet and set index.jsp as a welcome file . The code of the web.xml file 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>

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

</servlet>

<servlet-mapping>

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

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

</servlet-mapping>

<welcome-file-list>

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

</welcome-file-list>

<taglib>

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

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

</taglib>

</web-app>

Step 3:

Now we will create dispatcher-servlet.xml inside the /WEB-INF/ folder. In this dispatcher-servlet.xml we will include some code that are important to use UrlBasedViewResolver. The UrlBasedViewresolver much similar to the InternalResourceViewresolver. The code for the UrlBasedViewResolver in the dispatcher-servlet.xml is: 

<bean id="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>

Through this view resolver we have direct mapping the url to actual view. In the Spring MVC, every controller return a view after that the view resolver append the prefix and suffix in this view and show the view as response. The full code of the dispatcher-servlet.xml is:

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

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

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

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">

<property name="mappings">

<props>

<prop key="/urlbasedviewresolver.html">urlBasedViewResolverController</prop>

</props>

</property>

</bean>

<bean name="urlBasedViewResolverController"

class="net.roseindia.web.UrlbasedViewResolver">

</bean>

<bean id="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>

Step 4:

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

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

pageEncoding="ISO-8859-1"%>

<html>

<head>

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

<title>URL Based View Resolver Example</title>

</head>

<body>

<center>

<table>

<tr>

<td>

<b>${message}</b>

</td>

</tr>

</table>

</center>

</body>

</html>

Step 5:

Now we will create a controller class for handle the user request that implements the AbstractController. The controller class name is UrlbasedViewResolver.java. This controller class return a message and ModelAndView. The code of the UrlbasedViewResolver.java is:

package net.roseindia.web;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.AbstractController;
public class UrlbasedViewResolver extends AbstractController 
@Override
protected ModelAndView handleRequestInternal(HttpServletRequest request, HttpServletResponse response)throws Exception {
String Message = "This is URL Based View Resolver Test Example."
ModelAndView modelAndView = new ModelAndView("message");
 modelAndView.addObject("message", Message)
 return modelAndView;
  }
}

Step 6:

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

Now user click on this link and see like:

Download code

Download this example code

Advertisement

If you enjoyed this post then why not add us on Google+? Add us to your Circles



Liked it!  Share this Tutorial


Follow us on Twitter, or add us on Facebook or Google Plus to keep you updated with the recent trends of Java and other open source platforms.

Posted on: May 2, 2011

Related Tutorials

 

Post your Comment


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 
Comments:0
DMCA.com