How to automate body import with Apache Tiles and Spring MVC

1.6k views Asked by At

I have the following tiles-def.xml:

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE tiles-definitions PUBLIC
   "-//Apache Software Foundation//DTD Tiles Configuration 2.0//EN"
   "http://tiles.apache.org/dtds/tiles-config_2_0.dtd">
<tiles-definitions>
<definition name="default" template="/WEB-INF/jsp/layout.jsp">
    <put-attribute name="title" value="my webapp" />
    <put-attribute name="header" value="/WEB-INF/jsp/header.jsp" />
    <put-attribute name="menu" value="/WEB-INF/jsp/menu.jsp" />
    <put-attribute name="body" value="/WEB-INF/jsp/hello.jsp" />
    <put-attribute name="footer" value="/WEB-INF/jsp/footer.jsp" />
</definition>

<definition name="contact" extends="default">
    <put-attribute name="body" value="/WEB-INF/jsp/contact.jsp" />
</definition>

<definition name="hello" extends="default">
    <put-attribute name="body" value="/WEB-INF/jsp/hello.jsp" />
</definition>
</tiles-definitions>

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?)

Thank you!

EDIT: More details of what I want to do:

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.

What I would like to do: Just have one default definition that automatically chose the page to display, something like this:

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE tiles-definitions PUBLIC
   "-//Apache Software Foundation//DTD Tiles Configuration 2.0//EN"
   "http://tiles.apache.org/dtds/tiles-config_2_0.dtd">
<tiles-definitions>
<definition name="default" template="/WEB-INF/jsp/layout.jsp">
    <put-attribute name="title" value="my webapp" />
    <put-attribute name="header" value="/WEB-INF/jsp/header.jsp" />
    <put-attribute name="menu" value="/WEB-INF/jsp/menu.jsp" />
    if(myPageName exists){
        <put-attribute name="body" value="/WEB-INF/jsp/" + myPageName + ".jsp" />
    else{
        <put-attribute name="body" value="/WEB-INF/jsp/error404.jsp" />
    }
    <put-attribute name="footer" value="/WEB-INF/jsp/footer.jsp" />
</definition>
</tiles-definitions>

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").

How can I do this?

1

There are 1 answers

0
James On

Check out the Dynamic Tiles Example on the springbyexample.org page. The DynamicTilesView does exactly what you want (see example config here). The full Dynamic Tiles Example can be found here on github