16

I'm using Tiles and I'd like an attribute defined for a page to be readable inside of one of the included subpages as follows:

tiles-definitions.xml:

<definition name="page" template="/WEB-INF/tiles/layout/page.jsp">
  <put-attribute name="header" value="/WEB-INF/jsp/_include/header.jsp"/>
  <put-attribute name="footer" value="/WEB-INF/jsp/_include/footer.jsp"/>
</definition>

<definition name="tutorial" extends="page">
  <put-attribute name="title" value="Tutorial"/>
  <put-attribute name="body" value="/WEB-INF/jsp/tutorial.jsp"/>
</definition>

The page.jsp represents a simple page structure:

<html>
  <head><title><tiles:getAsString name="title"/></title></head>
  <body>
      <tiles:insertAttribute name="header"/>
      <tiles:insertAttribute name="body"/>
      <tiles:insertAttribute name="footer"/>
  </body>
</html>

I'm trying to read the title also inside the "header" subpage but I get the following error:

org.apache.tiles.template.NoSuchAttributeException: Attribute 'title' not found.

Here is how I'm trying to access the attribute in header.jsp:

<tiles:getAsString name="title"/>
skaffman
  • 398,947
  • 96
  • 818
  • 769
stack-o-frankie
  • 457
  • 5
  • 15

2 Answers2

22

To propagate the title attribute to inner templates you need to add cascade="true" attribute to the title's <put-attribute> tag, like this:

<definition name="tutorial" extends="page">
  <put-attribute name="title" cascade="true" value="Tutorial"/>
  <put-attribute name="body" value="/WEB-INF/jsp/tutorial.jsp"/>
</definition>
m_vitaly
  • 11,856
  • 5
  • 47
  • 63
  • 1
    It worked for me as well. It's been awhile -- stack-o-frankie, can you accept this answer? –  Jul 13 '12 at 19:46
  • 1
    In addition if you use `` instead of `` then you can use the attribute in EL with `${attribute}` – Adam Nov 27 '14 at 11:06
5

Take care to upgrade the Tiles definition file to 2.1 min, as cascade is available since 2.1.

<!DOCTYPE tiles-definitions PUBLIC "-//Apache Software Foundation//DTD Tiles Configuration 2.1//EN" "http://tiles.apache.org/dtds/tiles-config_2_1.dtd">

tduchateau
  • 4,351
  • 2
  • 29
  • 40