You can integrate Tiles templates - like any other presentation technology - into web applications that use Spring. This section outlines how to do this.
org.springframework.web.servlet.view.tiles3
package.
Dependencies
To be able to use Tiles templates, you must add a dependency on Tiles version 3.0.1 or higher and its transitive dependencies to your project.
Configuration
To be able to use Tiles, you must configure it with definition files (for basic information about definitions and other Tiles concepts, see https://tiles.apache.org). In Spring this is done using TilesConfigurer
. The following ApplicationContext
configuration example shows how to do this:
<bean id="tilesConfigurer" class="org.springframework.web.servlet.view.tiles3.TilesConfigurer">
<property name="definitions">
<list>
<value>/WEB-INF/defs/general.xml</value>
<value>/WEB-INF/defs/widgets.xml</value>
<value>/WEB-INF/defs/administrator.xml</value>
<value>/WEB-INF/defs/customer.xml</value>
<value>/WEB-INF/defs/templates.xml</value>
</list>
</property>
</bean>
The previous example defined five files containing definitions. All files are located in the WEB-INF/defs
directory. When WebApplicationContext
is initialized, files are loaded and the definition factory is initialized. The Tiles templates included in the definition files can then be used as views in your Spring web application. To be able to use views, you need to provide a ViewResolver
, just like any other view technology in Spring: usually the convenient TilesViewResolver
.
You can set Tiles template definitions to specific locales by adding an underscore followed by the locale itself, as shown in the following example:
<bean id="tilesConfigurer" class="org.springframework.web.servlet.view.tiles3.TilesConfigurer">
<property name="definitions">
<list>
<value>/WEB-INF/defs/tiles.xml</value>
<value>/WEB-INF/defs/tiles_fr_FR.xml</value>
</list>
</property>
</bean>
With the previous configuration, tiles_fr_FR.xml
is used for queries with fr_FR
locales, and tiles.xml
is used by default.
UrlBasedViewResolver
The UrlBasedViewResolver
creates an instance of the given viewClass
for each view it needs to recognize. The following bean defines UrlBasedViewResolver
:
<bean id="viewResolver" class="org.springframework.web.servlet.view.UrlBasedViewResolver">
<property name="viewClass" value="org.springframework.web.servlet.view.tiles3.TilesView"/>
</bean>
SimpleSpringPreparerFactory
and SpringBeanPreparerFactory
As an additional feature, Spring also supports two special PreparerFactory
implementations for Tiles templates. For more information about how to use ViewPreparer
references in Tiles template definition files, see the Tiles documentation.
You can define a SimpleSpringPreparerFactory
to automatically discover and bind ViewPreparer
instances based on specified preparer classes, using Spring container callbacks, and also using configured BeanPostProcessors in Spring. If Spring annotation context configuration has been enabled, annotations in ViewPreparer
classes are automatically detected and applied. Note that this requires the presence of preparer classes in the Tiles template definition files, as is the case with PreparerFactory by default.
You can specify SpringBeanPreparerFactory
to operate on given preparer names (instead of classes), retrieving the corresponding Spring bean from the DispatcherServlet application context. In this case, the entire bean creation process is under the control of the Spring application context, allowing for explicit dependency injection configuration, scoped beans, and so on. Note that you need to define one Spring bean definition for each preparer name (as your Tiles template definitions do). The following example shows how to define the SpringBeanPreparerFactory
property for the TilesConfigurer
bean:
<bean id="tilesConfigurer" class="org.springframework.web.servlet.view.tiles3.TilesConfigurer">
<property name="definitions">
<list>
<value>/WEB-INF/defs/general.xml</value>
<value>/WEB-INF/defs/widgets.xml</value>
<value>/WEB-INF/defs/administrator.xml</value>
<value>/WEB-INF/defs/customer.xml</value>
<value>/WEB-INF/defs/templates.xml</value>
</list>
</property>
<!-- resolve preparer names as Spring bean definition names -->
<property name="preparerFactoryClass"
value="org.springframework.web.servlet.view.tiles3.SpringBeanPreparerFactory"/>
</bean>
GO TO FULL VERSION