title: How to automate body import with Apache Tiles and Spring MVC
body: <p>I have the following tiles-def.xml:</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="UTF-8" ?&gt;
  &lt;!DOCTYPE tiles-definitions PUBLIC
     "-//Apache Software Foundation//DTD Tiles Configuration 2.0//EN"
     "http://tiles.apache.org/dtds/tiles-config_2_0.dtd"&gt;
  &lt;tiles-definitions&gt;
  &lt;definition name="default" template="/WEB-INF/jsp/layout.jsp"&gt;
      &lt;put-attribute name="title" value="my webapp" /&gt;
      &lt;put-attribute name="header" value="/WEB-INF/jsp/header.jsp" /&gt;
      &lt;put-attribute name="menu" value="/WEB-INF/jsp/menu.jsp" /&gt;
      &lt;put-attribute name="body" value="/WEB-INF/jsp/hello.jsp" /&gt;
      &lt;put-attribute name="footer" value="/WEB-INF/jsp/footer.jsp" /&gt;
  &lt;/definition&gt;
  
  &lt;definition name="contact" extends="default"&gt;
      &lt;put-attribute name="body" value="/WEB-INF/jsp/contact.jsp" /&gt;
  &lt;/definition&gt;
  
  &lt;definition name="hello" extends="default"&gt;
      &lt;put-attribute name="body" value="/WEB-INF/jsp/hello.jsp" /&gt;
  &lt;/definition&gt;
  &lt;/tiles-definitions&gt;
  </code></pre>
  
  <p>As you can see, in all my pages, just the body changes. Is there a way to automate this? (so it automatically includes the jsp that has the name of the requested page, or an error page if it doesnt exist?)</p>
  
  <p>Thank you!</p>
  
  <p>EDIT: More details of what I want to do:</p>
  
  <p>How it is now:
  For each new page of my webapp, I have to add a new  to my tiles-def.xml. And everytime, the only thing it does is setting the body with my content page.</p>
  
  <p>What I would like to do:
  Just have one default definition that automatically chose the page to display, something like this:</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="UTF-8" ?&gt;
  &lt;!DOCTYPE tiles-definitions PUBLIC
     "-//Apache Software Foundation//DTD Tiles Configuration 2.0//EN"
     "http://tiles.apache.org/dtds/tiles-config_2_0.dtd"&gt;
  &lt;tiles-definitions&gt;
  &lt;definition name="default" template="/WEB-INF/jsp/layout.jsp"&gt;
      &lt;put-attribute name="title" value="my webapp" /&gt;
      &lt;put-attribute name="header" value="/WEB-INF/jsp/header.jsp" /&gt;
      &lt;put-attribute name="menu" value="/WEB-INF/jsp/menu.jsp" /&gt;
      if(myPageName exists){
          &lt;put-attribute name="body" value="/WEB-INF/jsp/" + myPageName + ".jsp" /&gt;
      else{
          &lt;put-attribute name="body" value="/WEB-INF/jsp/error404.jsp" /&gt;
      }
      &lt;put-attribute name="footer" value="/WEB-INF/jsp/footer.jsp" /&gt;
  &lt;/definition&gt;
  &lt;/tiles-definitions&gt;
  </code></pre>
  
  <p>Where myPageName would be the name of the page requested (If the URL was "http://www.mysite.com/contact.html", then myPageName would be "contact". If it was "http://www.mysite.com/info.html", then myPageName would be "info").</p>
  
  <p>How can I do this?</p>