Upgrade from Tiles 2 to Tiles 3 on Spring MVC 3 application
(P) Codever is an open source bookmarks and snippets manager for developers & co. See our How To guides to help you get started. Public bookmarks repos on Github ⭐🙏
I’ve just upgraded the web application that powers Podcastpedia.org to use Apache Tiles 3 instead of Tiles 2. I am particularly interested in the Tiles integration with Velocity for email generation.
Note: You need at least the Spring 3.2 release for this to work.
This occured in three small steps:
- upgrade maven dependencies
- upgrade tiles elements in the Spring application context
- upgrade tiles dtd version
1. Upgrade maven dependencies
What I had to do was just replace the ${tiles.version} in the maven pom.xml file from 2.2.2 to 3.0.1 for the Tiles dependencies I use, and now it looks like this:
<dependency>
<groupId>org.apache.tiles</groupId>
<artifactId>tiles-extras</artifactId>
<version>${tiles.version}</version>
</dependency>
<dependency>
<groupId>org.apache.tiles</groupId>
<artifactId>tiles-jsp</artifactId>
<version>${tiles.version}</version>
</dependency>
<dependency>
<groupId>org.apache.tiles</groupId>
<artifactId>tiles-servlet</artifactId>
<version>${tiles.version}</version>
</dependency>
<dependency>
<groupId>org.apache.tiles</groupId>
<artifactId>tiles-template</artifactId>
<version>${tiles.version}</version>
</dependency>
<dependency>
<groupId>org.apache.tiles</groupId>
<artifactId>tiles-el</artifactId>
<version>${tiles.version}</version>
</dependency>
2. Upgrad Tiles configuration in Spring application context
The next thing was to upgrade to version 3 the tilesViewResolver and tilesConfigurer in the Spring application context:
....................
<!-- Convenience subclass of UrlBasedViewResolver that supports TilesView (i.e. Tiles definitions) and custom subclasses of it. -->
<!-- Don't forget to set the order if you declared other ViewResolvers
-->
<bean id="tilesViewResolver" class="org.springframework.web.servlet.view.UrlBasedViewResolver">
<property name="viewClass" value="org.springframework.web.servlet.view.tiles3.TilesView" />
<property name="order" value="#{contentNegotiatingResolver.order+1}" />
</bean>
<bean id="viewResolver" class="org.springframework.web.servlet.view.ResourceBundleViewResolver">
<property name="basename" value="views"/>
<property name="order" value="#{tilesViewResolver.order+1}" />
</bean>
<!-- Helper class to configure Tiles 2.x for the Spring Framework -->
<!-- See https://static.springsource.org/spring/docs/3.0.x/javadoc-api/org/springframework/web/servlet/view/tiles2/TilesConfigurer.html -->
<!-- The actual tiles templates are in the tiles-definitions.xml -->
<bean id="tilesConfigurer" class="org.springframework.web.servlet.view.tiles3.TilesConfigurer">
<property name="definitions">
<list>
<value>/WEB-INF/tile-defs/templates.xml</value>
<value>/WEB-INF/tile-defs/definitions.xml</value>
</list>
</property>
</bean>
....................
3. Upgrade tiles.dtd version
In the Tiles definitions templates upgrade to Tiles 3.0 dtd
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE tiles-definitions PUBLIC "-//Apache Software Foundation//DTD Tiles Configuration 3.0//EN" "https://tiles.apache.org/dtds/tiles-config_3_0.dtd"> <tiles-definitions> ......... </tiles-definitions>
That’s it.
P.S.
For a complete example of Tiles integration with Spring MVC see my post – Spring 3 and Tiles 2 Integration