Wild card tiles defs with spring MVC

A while ago I wrote about using Spring MVC to automatically scan for xml tiles definitions. This works beautifully but I later realised that you could go one better and do away with individual definitions in favour of wildcards. Essentially implementing your own convention over configuration.

The spring setup is the same as in the previous article. You need to have:

  • A tiles view resolver
  • A spring bean of type TilesConfigurer with completeAutoload set to true
  • You need to include tiles 2.2 or greater and tiles-extras.jar

This is explained in greater detail in the previous post so here’s the link again if you need more detail.
Auto scanning tiles defs with spring MVC

Setting up wildcards definitions
First you need to decide on your folders structure. I’ve put everything under WEB-INF/jsp. Under this folder I have one folder called template which holds all the jsp fragments for headers and footers and the few xml tiles defs. The tiles definitions will still be automatically scanned, there will just be fewer of them. The second folder is called views which contains the actual pages with body content.

I’m using a base definition which I plan to override for different sections of the site. Each section can then have it’s own sidebar coming from a different jsp fragment. For example the admin section of the site will override the base definition with an admin specific sidebar. All admin related pages should use this admin definition.

Here’s the tiles definitions file with a simple base def and the overridden admin def.

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE tiles-definitions PUBLIC
       "-//Apache Software Foundation//DTD Tiles Configuration 2.0//EN"

    <definition name="base.definition" template="/WEB-INF/jsp/template/layout.jsp">
        <put-attribute name="title" value="" />
        <put-attribute name="header" value="/WEB-INF/jsp/template/header.jsp" />
        <put-attribute name="sidebar" value="/WEB-INF/jsp/template/sidebars/sidebar.jsp" />
        <put-attribute name="body" value="" />
        <put-attribute name="footer" value="/WEB-INF/jsp/template/footer.jsp" />
    <definition name="admin.definition" extends="base.definition">
        <put-attribute name="sidebar" value="/WEB-INF/jsp/template/sidebars/admin-sidebar.jsp" />
    <definition name="WILDCARD:admin/*" extends="admin.definition">
       <put-attribute name="title" value=" - {1}" />
       <put-attribute name="body" value="/WEB-INF/jsp/views/admin/{1}.jsp" />

We’re using the extends attribute to inherit as much as possible from the base definition. The admin base definition just overrides the sidebar location. We could if we wanted use this as a base for defining individual tiles views. Instead we have a definition with a name of “WILDCARD:admin/*”. This means any tiles view requested where the name starts admin/* will use this definition. The rest of the tile name is used to replace the {1} replacement variables.

So for example, if your controller returns a string of “admin/user” spring will look for a tile with this name. It should find the wildcard def and insert the title as “user” and the body jsp as /WEB-INF/jsp/views/admin/user.jsp.

There’s a bit more info in the tiles documentation.


One thought on “Wild card tiles defs with spring MVC

  1. Pingback: Auto scanning tiles defs with spring MVC | Ben Thurley

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