Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 993
David Moreno García Avatar asked Sep 12 '26 04:09

David Moreno García


2 Answers

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

like image 111
Ken de Guzman Avatar answered Sep 15 '26 02:09

Ken de Guzman


You may need to declare the class. You'll also need id.

<tilesx:useAttribute name="active" id="active" class="java.lang.String"/>

Also if there are other tiles definitions in your app other than "home" and if any of them do not define an "active" attribute, you may want to set "ignore" to true as part of your definition to avoid runtime errors. More here:https://tiles.apache.org/framework/tiles-jsp/tlddoc/index.html

like image 33
Angad Avatar answered Sep 15 '26 00:09

Angad