Search code examples
springspring-mvcapache-tiles

Attribute not found on Apache Tiles


I'm using Apache Tiles on a Sprint MVC app and I have this tiles.xml:

<tiles-definitions>
    <definition name="defaultLayout" template="/WEB-INF/tiles/template/defaultLayout.jsp">
        <put-attribute name="title" value="" />
        <put-attribute name="header" value="/WEB-INF/tiles/template/header.jsp" />
        <put-attribute name="content" value=""/>
        <put-attribute name="footer" value="/WEB-INF/tiles/template/footer.jsp" />
    </definition>

    <definition name="home" extends="defaultLayout">
        <put-attribute name="title" value="Alsa" />
        <put-attribute name="content" value="/WEB-INF/pages/home.jsp" />
        <put-attribute name="active" value="index" />
    </definition>
</tiles-definitions>

What I'm trying to do is use the attribute active to add a class to active menu item. For that I have this in header.jsp:

<%@ taglib uri="http://tiles.apache.org/tags-tiles-extras" prefix="tilesx" %>

<tilesx:useAttribute name="active" />

The problem is that everytime I try to render the page I get this error:

org.apache.tiles.template.NoSuchAttributeException: Error importing attributes. Attribute 'active' is null

What am I doing wrong?


Solution

  • The trick here is to add cascade=true to your attribute so that it will be available to nested definitions and templates.

     <definition name="home" extends="defaultLayout">
            <put-attribute name="title"   value="Alsa" />
            <put-attribute name="content" value="/WEB-INF/pages/home.jsp" />
            <put-attribute name="active"  value="index" cascade="true"/>
        </definition>
    

    See: https://tiles.apache.org/framework/tutorial/advanced/nesting-extending.html