Wildcard support

By default, Tiles 2.2 supports wildcards in definition names. Wilcards help a lot in writing less code to declare your definitions.

There are two styles:

  • the wildcard-based style (available by default):
    <definition name="test.definition*.message*" template="/layout{1}.jsp">
        <put-attribute name="title"  value="This definition has a message: {2}."/>
        <put-attribute name="header" value="/header.jsp"/>
        <put-attribute name="body"   value="/body.jsp"/>
    </definition>

Using different pattern matching languages

In Tiles 2.2 it is possible to use different pattern matching languages at the same time. Tiles supports Wildcard and Regular Expressions natively.

To configure it, override the createPatternDefinitionResolver method of BasicTilesContainerFactory this way:

@Override
protected <T> PatternDefinitionResolver<T> createPatternDefinitionResolver(
        Class<T> customizationKeyClass) {
    DefinitionPatternMatcherFactory wildcardFactory = new WildcardDefinitionPatternMatcherFactory();
    DefinitionPatternMatcherFactory regexpFactory = new RegexpDefinitionPatternMatcherFactory();
    PrefixedPatternDefinitionResolver<T> resolver = new PrefixedPatternDefinitionResolver<T>();
    resolver.registerDefinitionPatternMatcherFactory("WILDCARD", wildcardFactory);
    resolver.registerDefinitionPatternMatcherFactory("REGEXP", regexpFactory);
    return resolver;
}

Now you can still use Wildcard notation, but remember to add the WILDCARD: prefix:

<definition name="WILDCARD:test.definition*.message*" template="/layout{1}.jsp">
    <put-attribute name="title"  value="This definition has a message: {2}."/>
    <put-attribute name="header" value="/header.jsp"/>
    <put-attribute name="body"   value="/body.jsp"/>
</definition>

And, if you want to use Regular Expressions:

<definition name="REGEXP:test\.definition(.*)\.message(.*)" template="/layout{1}.jsp">
    <put-attribute name="title"  value="This definition has a message: {2}."/>
    <put-attribute name="header" value="/header.jsp"/>
    <put-attribute name="body"   value="/body.jsp"/>
</definition>

In both cases, if you insert a definition that matches the definition, for example:

<tiles:insertDefinition name="test.definitionOne.messageThisIsAMessage" />

The definition is automatically mapped, replacing placeholders, marked by curly brackets, with the value of matched definitions. In this case:

<definition name="test.definitionOne.messageThisIsAMessage" template="/layoutOne.jsp">
    <put-attribute name="title"  value="This definition has a message: ThisIsAMessage."/>
    <put-attribute name="header" value="/header.jsp"/>
    <put-attribute name="body"   value="/body.jsp"/>
</definition>