UrlFileNameViewController Example

If user don't want to include any logical operation on request and redirect to some resource then user used UrlFilenameViewController that's transform the virtual path of a URL into a view name and provide a view to display as a user interface.

Ads

Tutorials   
Spring 4 Introduction and example Features of Spring Framework 5 How to make Spring web Login form? How to make Simple form in Spring web? Spring, Hibernate login and registration application Spring Framework Tutorial for beginners with examples Spring Framework for Apache Hadoop 2.3.0 GA released Spring Framework 4.1 GA is released with major features Why to use Spring Framework? Spring Framework 4.1 - First Release candidate available Spring IO Platform 1.0.0 Released Spring 4: Login Form using Spring MVC and Hibernate Example Spring 4 MVC Login form Example with source code Spring 4 MVC Hello World Example: Spring 4 MVC Tutorial will full source code Spring Web MVC Application Error:ClassNotFoundException: DispatcherServlet on deploying Features of Spring 4 Spring Framework 4.0 released Spring Framework 4: Spring Framework 4 Tutorials and Example Spring Integration 3.0 Release is released and available for download Spring Tutorial for Beginners Java Springs Framework Tutorial Spring Architecture Spring Framework Tutorials database spring registration form Spring Login Example Roseindia Spring Tutorial Spring Tutorial Spring 3.2 MVC insert and retrieve blob from the database The hidden tag The errors tag net.roseindia.dao net.roseindia.service net.roseindia.model net.roseindia.controller Spring 3.2 MVC Hibernate Example Spring 3.2 MVC, Upload File in a specific folder Spring 3.2 MVC Form Handling The textarea tag The options tag The option tag The select tag The radiobuttons tag The radiobutton tag The checkboxes tag mvc:default-servlet-handler Static Resources Configuration View Controllers Configuration View resolvers Spring 3.2 MVC Hello World Example Content Negotiation Configuration
Ads

UrlFileNameViewController Example

     

UrlFileNameViewController Example in Spring 2.5 Web MVC Framework

If user don't want to include any logical operation on request and redirect to some resource then user used  UrlFilenameViewController that's transform the virtual path of a URL into a view name and provide a view to display as a user interface. In this example we will discuss about UrlFileNameViewController.

Let's start developing the example program that uses UrlFileNameViewController class. Follow the steps mentioned below and develop the example program. You can also download source code of UrlFileNameViewController example discussed here from the download link provided at the end of this tutorial.

Step 1:

Now we will create index.jsp file inside the project WebContent folder that'll have two hyperlink to send user requests for "MyAddress.html" and "MyOfficeAddress.html". After that we will set this file as a welcome file in the web.xml configuration file. The code of the index.jsp is:

<html>

<head></head>

<body>

<h3>UrlFileNameViewController Example</h3>

<h4><a href="MyAddress.html">My Home Address</a></h4><br/>

<h4><a href="MyOfficeAddress.html">My Office Address</a></h4>

</body>

</html>

Step 2:

Now we will configured the web.xml file for the DispatcherServlet and will have welcome file setting. 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>

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

</web-app>

Step 3:

Now we will create a dispatcher-servlet.xml file inside the /WEB-INF/ folder that will have all configuration beans for the each and every request and set controllers for each request. we will create a bean for the UrlFileNameViewController for provide the control the requests. The code for use UrlFileNameViewController in the dispatcher-servlet.xml is:

<bean id="staticViewController" class="org.springframework.web.servlet.mvc.UrlFilenameViewController"/>

 Now we will set the viewResolver that will resolved the suffix and prefix for the response. The code for viewResolver in the dispatcher-servlet.xml is:

<bean id="viewResolver"

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

<property name="prefix">

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

</property>

<property name="suffix">

<value>.jsp</value>

</property>

</bean>

Now we will used UrlFolenameViewController with the urlMapping. The code of the urlMapping with UrlFolenameViewController in dispatcher-servlet.xml is:

<bean id="urlMapping"

class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">

<property name="interceptors">

<list>

<ref local="localeChangeInterceptor"/>

</list>

</property>

<property name="urlMap">

<map>

<entry key="/*.html">

<ref bean="staticViewController"/>

</entry>

</map>

</property>

</bean>

The full code of the dispatcher-servlet.xml for the UrlFileNameViewController 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 id="staticViewController"

class="org.springframework.web.servlet.mvc.UrlFilenameViewController"/>

<bean id="viewResolver"

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

<property name="prefix">

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

</property>

<property name="suffix">

<value>.jsp</value>

</property>

</bean>

<bean id="urlMapping"

class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">

<property name="interceptors">

<list>

<ref local="localeChangeInterceptor"/>

</list>

</property>

<property name="urlMap">

<map>

<entry key="/*.html">

<ref bean="staticViewController"/>

</entry>

</map>

</property>

</bean>

<bean id="localeChangeInterceptor" class=
"org.springframework.web.servlet.i18n.LocaleChangeInterceptor"
>

<property name="paramName" value="hl"/>

</bean>

</beans>

Step 4:

Now we will create two jsp file inside /WEB-INF/ folder that's name "MyAddress.jsp" and "MyOfficeAddress.jsp" for redirect the user on.

The code of the MyAddress.jsp is:

<html>

<head>

</head>

<body>

<h2>My Home Address</h2>

<h3> New Delhi </h3>

</body>

</html>

The code of the MyOfficeAddress.jsp is:

<html>

<head>

</head>

<body>

<h2>My Office Address</h2>

<h3>New Delhi</h3>

</body>

</html>

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

If user click on "My Home Address" link then output is:

If user click on "My Office Address" link then output is:

Download Code

Download this code

Advertisements

Share on Google+Share on Google+

UrlFileNameViewController Example

Posted on: October 6, 2010 If you enjoyed this post then why not add us on Google+? Add us to your Circles

Advertisements

 

Discuss: UrlFileNameViewController Example  

Post your Comment


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

Ads

 

Ads