/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.<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.<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. 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
Nam Ha Minh 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.