How to load Tiles definitions programmatically

Introduction

When implementing some sort of web application’s plugin architecture in which the layout is based on the Tiles framework (https://tiles.apache.org/) there could be the need to reload new tiles definitions on the fly.

How to load Tiles definitions programmatically

To load new tiles definitions first of all we get the Tiles container passing the servlet context to the getContainer method of TilesAccess class:
TilesContainer container = TilesAccess.getContainer(servletContext);

Then, assuming it is a BasicTilesContainer we get the definition’s factory out of it with the following:

BasicTilesContainer basic = (BasicTilesContainer) container;
DefinitionsFactory factory = basic.getDefinitionsFactory();

And, if we have the list of our tile definitions files in a variable called tilesFilesToLoad we can load them by the following code:

if (factory instanceof ReloadableDefinitionsFactory) {
                ReloadableDefinitionsFactory reloadableDefFactory = 
                	(ReloadableDefinitionsFactory) factory;
                for (File file : tilesFilesToLoad) {
                    URL source = new URL("file://localhost/" 
+ file.getAbsolutePath());
                    ((UrlDefinitionsFactory) reloadableDefFactory)
.addSource(source);
                    rFactory.refresh();
                }
}

This way our new definitions are loaded and available for the application use.

How to load Tiles definitions programmatically last modified: 2015-05-01T23:29:08+00:00 by Mario Casari

One thought on “How to load Tiles definitions programmatically

Leave a Reply

Your email address will not be published. Required fields are marked *