This solution picks up all xml files from given location and seems to be working with Tiles 2.1.2.
config:
Code:
<bean id="tilesConfigurer"
class="com.xxx.web.context.TilesConfigurer">
<property name="definitionPath" value="/WEB-INF/tiles-defs"></property>
<property name="definitionPattern" value="^.*\.xml$"></property>
</bean>
java:
Code:
import java.util.ArrayList;
import java.util.Enumeration;
import java.util.List;
import java.util.Properties;
import java.util.Set;
import javax.servlet.ServletConfig;
import javax.servlet.ServletContext;
import org.apache.log4j.Logger;
import org.apache.tiles.TilesApplicationContext;
import org.apache.tiles.TilesContainer;
import org.apache.tiles.TilesException;
import org.apache.tiles.access.TilesAccess;
import org.apache.tiles.context.AbstractTilesApplicationContextFactory;
import org.apache.tiles.context.ChainedTilesApplicationContextFactory;
import org.apache.tiles.context.ChainedTilesRequestContextFactory;
import org.apache.tiles.definition.DefinitionsFactory;
import org.apache.tiles.definition.UrlDefinitionsFactory;
import org.apache.tiles.factory.AbstractTilesContainerFactory;
import org.apache.tiles.factory.TilesContainerFactory;
import org.apache.tiles.preparer.BasicPreparerFactory;
import org.apache.tiles.servlet.context.ServletTilesApplicationContext;
import org.apache.tiles.web.util.ServletContextAdapter;
import org.springframework.beans.factory.DisposableBean;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.util.StringUtils;
import org.springframework.web.context.ServletContextAware;
import org.springframework.web.servlet.view.tiles2.SpringLocaleResolver;
import com.xxx.utils.CollectionsUtils;
public class TilesConfigurer implements ServletContextAware, InitializingBean, DisposableBean {
protected Logger logger = Logger.getLogger(TilesConfigurer.class);
private ServletContext servletContext;
private TilesApplicationContext tilesContext;
private String definitionPath;
private String definitionPattern;
private final Properties tilesPropertyMap = new Properties();
public TilesConfigurer() {
this.tilesPropertyMap.put(
AbstractTilesContainerFactory.CONTAINER_FACTORY_INIT_PARAM,
TilesContainerFactory.class.getName());
this.tilesPropertyMap.put(
AbstractTilesApplicationContextFactory.APPLICATION_CONTEXT_FACTORY_INIT_PARAM,
ChainedTilesApplicationContextFactory.class.getName());
this.tilesPropertyMap.put(
TilesContainerFactory.REQUEST_CONTEXT_FACTORY_INIT_PARAM,
ChainedTilesRequestContextFactory.class.getName());
this.tilesPropertyMap.put(
TilesContainerFactory.DEFINITIONS_FACTORY_INIT_PARAM,
UrlDefinitionsFactory.class.getName());
this.tilesPropertyMap.put(
TilesContainerFactory.PREPARER_FACTORY_INIT_PARAM,
BasicPreparerFactory.class.getName());
this.tilesPropertyMap.put(
UrlDefinitionsFactory.LOCALE_RESOLVER_IMPL_PROPERTY,
SpringLocaleResolver.class.getName());
}
@Override
public void setServletContext(ServletContext servletContext) {
this.servletContext = servletContext;
}
public void setDefinitionPath(String definitionPath) {
this.definitionPath = definitionPath;
}
public void setDefinitionPattern(String definitionPattern) {
this.definitionPattern = definitionPattern;
}
/**
* Set the Tiles definitions, i.e. the list of files containing the definitions.
* Default is "/WEB-INF/tiles.xml".
*/
public void setDefinitions(String[] definitions) {
if (definitions != null) {
String defs = StringUtils.arrayToCommaDelimitedString(definitions);
if (logger.isInfoEnabled()) {
logger.info("TilesConfigurer: adding definitions [" + defs + "]");
}
this.tilesPropertyMap.put(DefinitionsFactory.DEFINITIONS_CONFIG, defs);
}
}
public void init() {
List<String> definitions = new ArrayList<String>();
getResources(definitionPath, definitions);
String[] defs = new String[definitions.size()];
definitions.toArray(defs);
setDefinitions(defs);
}
public void afterPropertiesSet() throws TilesException {
init();
ServletContextAdapter adaptedContext = new ServletContextAdapter(new DelegatingServletConfig());
tilesContext = new ServletTilesApplicationContext(adaptedContext);
AbstractTilesContainerFactory factory = AbstractTilesContainerFactory.getTilesContainerFactory(tilesContext);
TilesContainer container = factory.createContainer(tilesContext);
TilesAccess.setContainer(tilesContext, container);
}
@SuppressWarnings("unchecked")
private void getResources(String path, List<String> definitions) {
Set<String> paths = servletContext.getResourcePaths(path);
if (!CollectionsUtils.isEmpty(paths)) {
for (String lPath : paths) {
if (lPath.matches(definitionPattern)) {
definitions.add(lPath);
} else
getResources(lPath, definitions);
}
}
}
@Override
public void destroy() throws Exception {
TilesAccess.setContainer(this.tilesContext, null);
}
/**
* Internal implementation of the ServletConfig interface, to be passed
* to the wrapped servlet. Delegates to ServletWrappingController fields
* and methods to provide init parameters and other environment info.
*/
private class DelegatingServletConfig implements ServletConfig {
public String getServletName() {
return "TilesConfigurer";
}
public ServletContext getServletContext() {
return servletContext;
}
public String getInitParameter(String paramName) {
return tilesPropertyMap.getProperty(paramName);
}
@SuppressWarnings("unchecked")
public Enumeration getInitParameterNames() {
return tilesPropertyMap.keys();
}
}
}