Richard’s Weblog

March 3, 2009

Apache Tiles 2, definitions in the classpath

Filed under: template system,Web development — Richard @ 1:23 pm
Tags: , ,

Following a previous post where I was learning Tiles 2, I had to try packaging some definition configuration files in a java archive for my project. At this time, the Tiles documentation seemed to say it’s possible to do it like this :

<?xml version="1.0" encoding="ISO-8859-1"?>
<web-app xmlns="http://java.sun.com/xml/ns/javaee"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
    version="2.5">

    <context-param>
<param-name>
            org.apache.tiles.definition.DefinitionsFactory.DEFINITIONS_CONFIG
        </param-name>
<param-value>
            /myTilesConfigFile.xml
        </param-value>
    </context-param>
	<listener>
	<listener-class>org.apache.tiles.web.startup.TilesListener</listener-class>
    </listener>

</web-app>

I tried it under Tomcat 6, putting myTilesConfigFile.xml in the root of the classpath in a JAR located in WEB-INF/lib, but could not manage to get it loaded by Tiles. The only way I found was to follow their tutorial about XML definition files using wildcards :

<?xml version="1.0" encoding="ISO-8859-1"?>
<web-app xmlns="http://java.sun.com/xml/ns/javaee"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
    version="2.5">

    <context-param>
<param-name>
            org.apache.tiles.definition.DefinitionsFactory.DEFINITIONS_CONFIG
        </param-name>
<param-value>
            classpath:/myTilesConfigFile.xml
        </param-value>
    </context-param>

    <context-param>
<param-name>org.apache.tiles.context.AbstractTilesApplicationContextFactory</param-name>
<param-value>
            org.apache.tiles.servlet.context.wildcard.WildcardServletTilesApplicationContextFactory
        </param-value>
    </context-param>
	<listener>
	<listener-class>org.apache.tiles.web.startup.TilesListener</listener-class>
    </listener>

</web-app>

It works because the WildcardServletTilesApplicationContextFactory uses Spring’s ressources system. This forced me to add spring-core.jar, spring-web.jar, spring-context.jar, spring-beans.jar and aopalliance.jar to my lib folder. But at least now I can use some naming convention for tiles to find my definition files.

Advertisements

2 Comments »

  1. Thx you saved my day with your trick to load the resource from the classpath.

    Seems Tiles needs a fix

    Comment by ntumba — July 10, 2009 @ 7:22 am | Reply

    • Ahaha, I’m glad it helped.

      Comment by Richard — July 22, 2009 @ 8:27 am | Reply


RSS feed for comments on this post. TrackBack URI

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Blog at WordPress.com.

%d bloggers like this: