Search code examples
javaspringjspapache-tiles

Apache tiles V3 includes in subpage


I'm trying to build a web application with apache tiles. I use apache tiles V3 with wild card support.

My tiles.xml looks as this:

<?xml version="1.0" encoding="ISO-8859-1" ?>
<!DOCTYPE tiles-definitions PUBLIC
        "-//Apache Software Foundation//DTD Tiles Configuration 3.0//EN"
        "http://tiles.apache.org/dtds/tiles-config_3_0.dtd">
<tiles-definitions>

    <definition name="*" template="/resources/tiles/main-layout.jsp">
        <put-attribute name="main" value="/resources/templates/{1}.jsp" />
        <put-attribute name="head-content" value="" />
    </definition>
    <definition name="*/*" template="/resources/tiles/main-layout.jsp">
        <put-attribute name="main" value="/resources/templates/{1}/{2}.jsp" />
        <put-attribute name="head-content" value="" />
    </definition>
    <definition name="*/*/*" template="/resources/tiles/main-layout.jsp">
        <put-attribute name="main" value="/resources/templates/{1}/{2}/{3}.jsp" />
        <put-attribute name="head-content" value="" />
    </definition>
</tiles-definitions>

I have a main page (main-layout.jsp) with head section and content section.

Head section:

<head> 
    <link rel="stylesheet" type="text/css" href="${baseCss}" />
    <link rel="stylesheet" type="text/css" href="${messages}" />
    <script src="${jquery}"></script>
    <script src="${jqueryui}"></script>
    <script src="${jquerycookie}"></script>
    <script src="${languagetoggle}"></script>
    <script src="${menuJs}"></script>
    <tiles:insertAttribute name="head-content"  />
</head>

Main section:

<section class="main-content">
     <tiles:insertAttribute name="main" />
</section>

My main section renders correct. I can just use Spring MVC to load via /hello/world and the file world.jsp gets loaded, from de hello folder.

No I want to add some extra css files to the head. My question is: How can I do that from de world.jsp file?

I already tried to add a tiles attribute and load it from the world.jsp file:

<tiles:putAttribute name="head-content">
    <spring:url value="/resources/base-theme/css/tables.css" var="tableCss" />
    <link rel="stylesheet" type="text/css" href="${tableCss}">   
</tiles:putAttribute>

But it does not work. When I google I always come on the same pages, where in de tiles.xml every page gets specified, but from tiles V3 with wildcard support it is not needed anymore. Could someone give me a hint how to get it done?


Solution

  • Please read the code below for your understanding. I have created a default tiles definition with a global css and js file. yourpage.jsp extends this definition and add two files to it: yourpage.js and yourpage.css

    tiles.xml

    <tiles-definitions>
        <definition name="app.base" template="/path/to/your/layout.jsp">
            <put-attribute name="title" value="Not Found" />
            <put-attribute name="header" value="/WEB-INF/tiles/header.jsp" />
            <put-attribute name="body" value="/WEB-INF/tiles/body.jsp" />
            <put-attribute name="footer" value="/WEB-INF/tiles/footer.jsp" />
            <put-list-attribute name="stylesheets">
                <add-attribute value="/static/resources/css/bootstrap.min.css" />           
                <add-attribute value="/static/resources/css/global.css" />
            </put-list-attribute>
            <put-list-attribute name="javascripts">
                <add-attribute value="/static/resources/js/jquery-2.1.4.min.js" />
                <add-attribute value="/static/resources/js/global.js" />
            </put-list-attribute>
        </definition>
        <definition name="yourpage" extends="app.base">
            <put-attribute name="title" value="Your Page" />
            <put-attribute name="body" value="/path/to/your/yourpage.jsp" />
            <put-list-attribute name="stylesheets" inherit="true">
                <add-attribute value="/static/resources/css/yourpage.css" />
            </put-list-attribute>
            <put-list-attribute name="javascripts" inherit="true">
                <add-attribute value="/static/resources/js/yourpage.js" />
            </put-list-attribute>
        </definition>
    </tiles-definitions>
    

    tiles.jsp

    <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
    <%@ taglib prefix="tiles" uri="http://tiles.apache.org/tags-tiles"%>
    <tiles:importAttribute name="stylesheets"/>
    <tiles:importAttribute name="javascripts"/>
    
    <!DOCTYPE html>
    <html lang="en">
    <head>
        <title>
            <tiles:insertAttribute name="title">
            </tiles:insertAttribute>
        </title>
    
        <!-- stylesheets-->
        <c:forEach var="css" items="${stylesheets}">
           <link rel="stylesheet" type="text/css" href="<c:url value="${css}"/>">
        </c:forEach>
    </head>
    
    <body>
        <header>
            <tiles:insertAttribute name="header" />
        </header>
        <div class="body">
            <tiles:insertAttribute name="body" />
        </div>
        <footer>
            <tiles:insertAttribute name="footer" />
        </footer>
    
        <!-- scripts-->
        <c:forEach var="script" items="${javascripts}">
            <script src="<c:url value="${script}"/>"></script>
        </c:forEach> 
    </body>
    </html>
    

    Hope this may be of help