Simple Tiles 2 Spring MVC Webapp

David Winterfeldt

2008


This is a simple example showing how to setup Spring MVC to use Tiles 2. Any request coming in mapped for Tiles processing will attempt to find a Tiles definition that matches the request and then render it.

1. Spring Configuration

The tilesConfigurer bean initializes tiles with all the tiles configuration files (more than one can be specified). The tilesViewResolver bean defines using Spring's TilesView which uses the url to lookup the Tiles definition and render it.

/WEB-INF/spring/webmvc-context.xml
                
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:p="http://www.springframework.org/schema/p"
       xmlns:mvc="http://www.springframework.org/schema/mvc"
       xsi:schemaLocation="http://www.springframework.org/schema/beans 
                           http://www.springframework.org/schema/beans/spring-beans.xsd
                           http://www.springframework.org/schema/mvc
                           http://www.springframework.org/schema/mvc/spring-mvc.xsd">

    <mvc:view-controller path="/index.html" />
    <mvc:view-controller path="/info/about.html" />

    <bean id="tilesConfigurer"
          class="org.springframework.web.servlet.view.tiles2.TilesConfigurer"
          p:definitions="/WEB-INF/tiles-defs/templates.xml" />

    <bean id="tilesViewResolver"
          class="org.springframework.web.servlet.view.UrlBasedViewResolver"
          p:viewClass="org.springframework.web.servlet.view.tiles2.TilesView" />

</beans>