title: Nested Thymeleaf Templates in Spring
body: <p><strong>Short version</strong></p>
  
  <p>How is one supposed to make nested templates in Thymeleaf when using Spring? It appears <a href="http://forum.thymeleaf.org/Using-conditional-logic-in-th-object-td4028232.html" rel="nofollow">asterisk notation is not supported</a> (<code>"*{mailingAddress}"</code>) inside <code>th:object</code> attributes in Spring. Is there a work-around / different tag to use?</p>
  
  <p><strong>Long version</strong></p>
  
  <p>For example, let's say I have these classes:</p>
  
  <pre><code>class Address { String street; }
  class Person { Address mailingAddress; Address shippingAddress; }
  class Order { int orderNo; Person customer; } 
  </code></pre>
  
  <p>So I make an <code>address.html</code> Thymeleaf template:</p>
  
  <pre><code>&lt;span th:text="*{street}"&gt;&lt;/span&gt;
  </code></pre>
  
  <p>We test it with a sample <code>Address</code>. Looks good.</p>
  
  <p>and I make a <code>person.html</code> Thymeleaf template that references the address like so:</p>
  
  <pre><code>&lt;span th:text="*{firstName}"&gt;&lt;/span&gt;
  &lt;span th:object="${person.shippingAddress}"&gt;
      &lt;span th:include="fragments/address :: address"&gt;&lt;/span&gt;
  &lt;/span&gt;
  </code></pre>
  
  <p>And we test it with an example person. I could even reference the same template and set the context to be the <code>${person.mailingAddress}</code>. So far so good.</p>
  
  <p>Now let's make our <code>Order</code> template. Only, hey, wait. Earlier, in our <code>person.html</code> file we said <code>${person.shippingAddress}</code> but <em>now</em> we need it to say <code>${order.customer.shippingAddress}</code>. <a href="http://forum.thymeleaf.org/Using-conditional-logic-in-th-object-td4028232.html" rel="nofollow">If I were not using Spring</a> I'd put the following into <code>person.html</code>:</p>
  
  <pre><code>&lt;span th:text="*{firstName}"&gt;&lt;/span&gt;
  &lt;span th:object="*{shippingAddress}"&gt;
      &lt;span th:include="fragments/address :: address"&gt;&lt;/span&gt;
  &lt;/span&gt;
  </code></pre>
  
  <p>That way, no matter what my path to getting here all I have to care about is that my current context has a <code>shippingAddress</code>. I could then use <code>person.html</code> directly as well as within my <code>order.html</code> template. </p>
  
  <p>Unfortunately I <em>am</em> in Spring, so I get the following exception:</p>
  
  <pre><code>org.thymeleaf.exceptions.TemplateProcessingException: 
      The expression used for object selection is *{shippingAddress},
      which is not valid: only variable expressions (${...}) are
      allowed in 'th:object' attributes in Spring-enabled environments. 
      (include:510)
  at org.thymeleaf.spring4.processor.attr.SpringObjectAttrProcessor.validateSelectionValue(SpringObjectAttrProcessor.java:73)
  at org.thymeleaf.standard.processor.attr.AbstractStandardSelectionAttrProcessor.getNewSelectionTarget(AbstractStandardSelectionAttrProcessor.java:69)
  at org.thymeleaf.processor.attr.AbstractSelectionTargetAttrProcessor.processAttribute(AbstractSelectionTargetAttrProcessor.java:61)
  </code></pre>
  
  <p>To move forward I must duplicate all my nested templates. In this example, I would have one <code>person.html</code> with <code>th:object="${person.mailingAddress}"</code> calling to <code>address.html</code>, and a duplicate of <code>person.html</code> called <code>orderCustomer.html</code> where we change the line to <code>th:object="${order.customer.mailingAddress}"</code>, but is otherwise identical.</p>
  
  <p>Is there a work-around out there that would let me re-use templates?</p>