Spring by Example's Dynamic Tiles 2 Spring MVC Module

David Winterfeldt

2008


The Dynamic Tiles 2 Spring MVC Module, version 1.1, dynamically renders a Tiles 2 template with Spring MVC. Any request coming in mapped for Tiles processing will use the default template and dynamically insert the body based on the URL.

Besides basic support for rendering dynamically rendering Tiles templates, there is also support for rendering Tiles fragments like AjaxTilesView and FlowAjaxTilesView in Spring JS and Spring Web Flow

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 DynamicTilesView which uses the url to lookup the Tiles definition, dynamically insert the body into the definition, and render it.

                
<?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.xsd">

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

    <bean id="tilesViewResolver"
     class="org.springframework.web.servlet.view.UrlBasedViewResolver">
        <property name="viewClass" value="org.springbyexample.web.servlet.view.tiles2.DynamicTilesView" />
        <property name="prefix" value="/WEB-INF/jsp/"/>
        <property name="suffix" value=".jsp"/>
    </bean>

    <bean id="urlMapping" class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
        <property name="mappings">
            <props>
                <prop key="/**/*.html">viewController</prop>
            </props>
        </property>
    </bean>

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

</beans>
                
            

For the AJAX requests to work correctly an AjaxUrlBasedViewResolver must be configured and FlowAjaxDynamicTilesView as the view class.

                
<bean id="tilesViewResolver" 
      class="org.springframework.js.ajax.AjaxUrlBasedViewResolver">  
    <property name="viewClass" value="org.springbyexample.web.servlet.view.tiles2.FlowAjaxDynamicTilesView" />
    <property name="prefix" value="/WEB-INF/jsp/" />
    <property name="suffix" value=".jsp" />
</bean>