Spring UrlFilenameViewController Example

Hope you guys have enjoyed previous article, MultiActionController Example. Now, let’s move on to UrlFilenameViewController. It transforms the virtual path of a URL into a view name and returns the view, that’s it. It is useful when you don’t need any logical operation on request and simply wants to redirect to some resource. For example, each site has ‘Contact Us’, ‘About Us’ pages. To map those pages UrlFilenameViewController is a good choice. Let’s see the example.

Initially index.jsp is viewed to user,

index.jsp

<html>

 <head>
 </head>
 <body>
 <h3> Spring Demo </h3>
 <h4>1) <a href="About_Us.do">About Us</a></h4>
 <h4>2) <a href="Contact_Us.do">Contact Us</a></h4>
 </body>

</html>

When submit button is pressed or hyperlink is clicked, flow will continue to WEB.XML from index.jsp

WEB.XML

<web-app>
<!-- Below is Spring DispatcherServlet that is configured with
FrontControllerServlet-servlet.xml -->

 <servlet>
 <servlet-name>FrontControllerServlet</servlet-name>
 <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> </servlet>
 <servlet-mapping>
 <servlet-name>FrontControllerServlet</servlet-name>
 <url-pattern>*.do</url-pattern>
 </servlet-mapping>

 <welcome-file-list>
 <welcome-file>index.do</welcome-file>
 </welcome-file-list>

</web-app>

WEB.XML instructs that, ‘*.do’ URLs are served by FrontControllerServlet. FrontControllerServlet is internally instantiated with the configuration file FrontControllerServlet-servlet.xml. (Remember Spring naming convention for DispatcherServlet configuration XML is ‘<DispatcherServlet name>-servlet.xml’)

FrontControllerServlet-servlet.xml

<beans>

 <!-- TO MAP URL TO PROPER CONTROLLER, 'SimpleUrlHandlerMapping' IS USED AS BELOW. ( Controller can be MultiAction/SimpleForm/UrlFilenameView etc.)
 -->
 <bean id="urlHandler">
 <property name="mappings">
 <map>
 <entry key="/index.do" value="urlFilenameViewController"/>
 <entry key="/About_Us.do" value="urlFilenameViewController"/>
 <entry key="/Contact_Us.do" value="urlFilenameViewController"/>
 </map>
 </property>
 </bean>

 <!-- BELOW, we instruct to change suffix to '.jsp' for any incoming request.
 In our case, '*.do' will be changed to '*.jsp'.
 e.g.
 index.do -> index.jsp
 About_Us.do -> About_Us.jsp
 Contact_Us.do -> Contact_Us.jsp
 -->
 <bean id="urlFilenameViewController">
 <property name="suffix" value=".jsp" />
 </bean>

</beans>

Inside FrontControllerServlet, request is given to ‘SimpleUrlHandlerMapping bean’ initially.
There request is routed to proper controller bean. In our case, ‘*.do’ are routed to UrlFilenameViewController bean.
Now, UrlFilenameViewController simply replaces extension from ‘.do’ to ‘.jsp’.
E.g. For index.do, it returns index.jsp to user. No need of any business logic to process request.
(‘UrlFilenameViewController’ has also one more property, named as ‘prefix’. Find out how it works. It’s simple)

About_Us.jsp

<html>
<head>
</head>
<body>
<a href="index.do">HOME</a>
<h2>About Us</h2>
<h3>aaaaaaaaaaaaaaaaa. </h3>
</body>
</html>

Contact_Us.jsp

<html>
 <head>
 </head>
 <body>
 <a href="index.do">HOME</a>
 <h2>Contact Us</h2>
 <h3>Address: Satelite Road, Ahemedabad, India.</h3>
 <h3>Phone: 00-00000000</h3>
 </body>
</html>

Download full source code here.

Related Posts

spring-framework-logo

Spring MultiActionController Example

Spring MVC Application Flow With Diagram

Ideal Way Of Exception Handling In Multi Layer Java Web Application

Top 10 Java Developer Forums

Leave a Reply









Ad

Like Us!