Monday, July 20, 2009

Integrating Tiles with Grails

One of our projects is required to use Tiles as its templating mechanism. Since we plan to use Grails for this project, we needed to find out if we can integrate Tiles with Grails (since Grails has its own templating mechanism, SiteMesh).

Didn't really find much info from googling, so i tried to download Tiles and manually integrate with a sample Grails app that i created. After some time, we were able to integrate Tiles successfully with my Grails app.

Here are the steps on how i did it:

1. Download Tiles 2.0 from http://tiles.apache.org/. Unzip the files.
2. Get the ff. files from the unzipped Tiles package, and place it in the lib folder of my webapp (note: not /web-app/WEB-INF/lib folder, but /lib folder directly).
3. We need to edit web.xml. Since it's auto-generated by grails, there's no web.xml in a new project. But you can make grails create the web.xml template for you to edit. Open console and type "grails install-templates" in your app folder. This will create a folder src/templates/war, where you can find the web.xml.
4. Edit web.xml and add the ff. entries:

<context-param>
<param-name>
org.apache.tiles.impl.BasicTilesContainer.DEFINITIONS_CONFIG
</param-name>
<param-value>
/WEB-INF/tiles-defs.xml
</param-value>
</context-param>

<filter>
<filter-name>Tiles Filter</filter-name>
<filter-class>org.apache.tiles.web.startup.TilesFilter</filter-class>
<init-param>
<param-name>
org.apache.tiles.impl.BasicTilesContainer.DEFINITIONS_CONFIG
</param-name>
<param-value>
/WEB-INF/tiles-defs.xml
</param-value>
</init-param>
</filter>

<filter-mapping>
<filter-name>Tiles Filter</filter-name>
<url-pattern>/*</url-pattern>
<dispatcher>REQUEST</dispatcher>
</filter-mapping>

<listener>
<listener-class>org.apache.tiles.web.startup.TilesListener</listener-class>
</listener>

<!-- Tiles servlet -->
<servlet>
<servlet-name>tiles</servlet-name>
<servlet-class>org.apache.tiles.web.startup.TilesServlet</servlet-class>
<init-param>
<param-name>
org.apache.tiles.impl.BasicTilesContainer.DEFINITIONS_CONFIG
</param-name>
<param-value>
/WEB-INF/tiles-defs.xml
</param-value>
</init-param>
<load-on-startup>2</load-on-startup>
</servlet>

We have added the ff: context-param, tiles filter, tiles listener, and tiles servlet.

5. Create a file WEB-INF/tiles-defs.xml (as defined in the web.xml entries above); this is where we will store the tiles definitions. E.g.:

<?xml version="1.0" encoding="ISO-8859-1" ?>
<!DOCTYPE tiles-definitions PUBLIC
"-//Apache Software Foundation//DTD Tiles Configuration 2.0//EN"
"http://tiles.apache.org/dtds/tiles-config_2_0.dtd">
<tiles-definitions>
<--tiles definitions here-->
</tiles-definitions>


After doing this, you should be able to use tiles normally as you would in any application. Note that you should specify jsp/gsp files in the tile def instead of just the url.
e.g. use
<definition name="myapp.homepage" template="/layouts/tiles.gsp">
instead of
<definition name="myapp.homepage" template="/layouts/tiles">
even if accessing /layouts/tiles will open tiles.gsp as convention by Grails.

Hope this helps. Note that this will only work for Grails 1.1 (beta version as of now), as Grails 1.0 does not support imported JSP tag libraries. I have only tried this with Tiles 2.0, though. I have heard that earlier version of Tiles is integrated in Struts, so not sure if we can use directly. Unfortunately for us, we will be using Tiles 1.1, hopefully we can integrate it later.

Try it out! Let me know if it works.

No comments:

Post a Comment