In Spring MVC development, sometimes you need to bypass controllers for a certain kind of URL patterns. For example, all URLs that end with /name.html will be resolved to /name view without going through handler methods of the controller.

In other words, those pages do not required business logic processing by the controller and serve as static pages instead. And a Spring’s view resolver (typically the InternalResourceViewResolver) will resolve the logical view name to an actual JSP page. The following example illustrates the idea:

/index.html -> index

/about.html -> about

/product/list.html -> /product/list

Spring helps to implement this resolution by providing the UrlFilenameViewController class which is a simple Controller implementation that transforms the virtual path of a URL into a view name and returns that view. All we need is to do some proper configurations in Spring’s application context file. The UrlFilenameViewController should be used in conjunction with a view resolver (typically the InternalResourceViewResolver) and a URL handler mapping (typically the SimpleUrlHandlerMapping). Let’s see two examples.

 

1. UrlFilenameViewController with XML Configuration

Firstly, configure a view resolver bean as follows:

<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
	<property name="prefix" value="/WEB-INF/views/" />
	<property name="suffix" value=".jsp" />
</bean>
This is very fundamental configuration in Spring MVC. It specifies that the Spring’s dispatcher servlet will resolve logical view names to physical view pages by appending /WEB-INF/views before the names and .jsp after the names.

Secondly, configure a simple UrlFilenameViewController bean as follows:

<bean id="urlViewController" 
	class="org.springframework.web.servlet.mvc.UrlFilenameViewController">
</bean>
And finally, configure a URL handler mapping as follows:

<bean class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
    <property name="mappings">
	<value>
	    /**/*.html=urlViewController
	</value>
    </property>
</bean>
This is the most important point, as we specify that all the URL patterns of /**/*.htmlwill be resolved by the configured UrlFilenameViewController.



In addition, we can specify prefix and suffix for the UrlFilenameViewControllerbean. For example:

<bean id="urlViewControllerWithPrefixAndSuffix" 
	class="org.springframework.web.servlet.mvc.UrlFilenameViewController">
	<property name="prefix" value="/dev/test_"/>
	<property name="suffix" value="_beta"/>
</bean>
And tell the URL handler mapping as follows:

<bean class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
    <property name="mappings">
	<value>
	    /**/*.html=urlViewController
	    /dev/*.html=urlViewControllerWithPrefixAndSuffix
	</value>
    </property>
</bean>
That means all URLs starts with /dev/name.htmlwill be resolved to view names /dev/test_name_beta. This is very useful in case you want to specify either prefix or suffix or both to the resolved view name.

 

2. UrlFilenameViewController with Java-based Configuration 

For those who prefer Java-based configuration, the following Java class does the same thing as the above XML configuration:

package net.codejava.spring;

import java.util.Properties;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.handler.SimpleUrlHandlerMapping;
import org.springframework.web.servlet.mvc.UrlFilenameViewController;
import org.springframework.web.servlet.view.InternalResourceViewResolver;

@Configuration
public class MvcConfiguration {
	
	@Bean
	public InternalResourceViewResolver getViewResolver() {
		InternalResourceViewResolver viewResolver = new InternalResourceViewResolver();
		viewResolver.setPrefix("/WEB-INF/views/");
		viewResolver.setSuffix(".jsp");
		return viewResolver;
	}
	
	@Bean(name = "urlViewController")
	public UrlFilenameViewController getUrlViewController() {
		UrlFilenameViewController urlViewController = new UrlFilenameViewController();
		return urlViewController;
	}
	
	@Bean
	public SimpleUrlHandlerMapping getUrlHandlerMapping() {
		SimpleUrlHandlerMapping handlerMapping = new SimpleUrlHandlerMapping();
		Properties mappings = new Properties();
		mappings.put("/**/*.html", "urlViewController");
		
		handlerMapping.setMappings(mappings);
		return handlerMapping;
	}
}
For your convenience, you can download the sample project in the attachment section.

 

Reference: Class UrlFilenameViewController

 

Related Spring View Tutorials:

 

Other Spring Tutorials:


About the Author:

is certified Java programmer (SCJP and SCWCD). He began programming with Java back in the days of Java 1.4 and has been passionate about it ever since. You can connect with him on Facebook and watch his Java videos on YouTube.



Attachments:
Download this file (UrlFilenameViewControllerExample.zip)UrlFilenameViewControllerExample.zip[Eclipse-Maven Project]18 kB

Add comment