How to prepare data for parent template when using SpringMVC and tiles

221 Views Asked by At

I am planning to use apache tiles in my SpringMVC application. The parent template have dynamic header and left side bar. Those contents also need model data from database. So, every request need to prepare those data such as user menus.

I don't think those data preparing should be put into every SpringMVC Controller's method.

So, is there any good practice to do it?

1

There are 1 best solutions below

0
On

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="/path/to/your/header.jsp" />
        <put-attribute name="body" value="/path/to/your/sidebar.jsp" />
        <put-attribute name="footer" value="/path/to/your/body.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="sidebar">
        <tiles:insertAttribute name="sidebar" />
    </div>
    <div class="body">
        <tiles:insertAttribute name="body" />
    </div>

    <!-- scripts-->
    <c:forEach var="script" items="${javascripts}">
        <script src="<c:url value="${script}"/>"></script>
    </c:forEach> 
</body>
</html>

Hope this may be of help