0

I am using apache tiles3 and I have a classic layout with default empty attributes:

<definition name="t-empty" template="/WEB-INF/tiles/template/empty.jsp"/>

//the base template is the site,
<definition name="base" template="/WEB-INF/tiles/layout/classic.jsp">
    <put-attribute name="res" value="t-empty"/>
    <put-attribute name="header" value="t-empty"/>
    <put-attribute name="body" value="t-empty"/>
    <put-attribute name="footer" value="t-empty"/>
</definition>

As shon, there is an attribute to be inserted named res which means the static javascript or css or inline styles.

And for the javascript, I can add them directly in the tile, but for the css or inline styles, they bave to be added inside the head section, that's why I add the res placeholder.

Now I have a layout which may use the bootstrape and backbone library, so I define it like this:

<definition name="single-page-bb" extends="base">
    <put-attribute name="header" value="/WEB-INF/tiles/template/header.jsp"/>
    <put-list-attribute name="res">
        <add-attribute value="/WEB-INF/tiles/template/res/jquery.jsp"/>
        <add-attribute value="/WEB-INF/tiles/template/res/backbone.jsp"/>
    </put-list-attribute>
</definition>

Then for a conrete page, I will put all the required attributes, and its own resources if any:

<definition name="user-list-page" extends="single-page-bb">
    <put-attribute name="header" value="/WEB-INF/tiles/fragment/user-list.jsp"/>
    <put-list-attribute name="res" inherit="true">
        <add-attribute           value="/WEB-INF/tiles/fragment/user-list-res.jsp"/> 
    </put-list-attribute>
</definition>

See the jsps: https://i.stack.imgur.com/qYAdM.png

It works, but I found that this is inconvenient, since I have to put the resources for the user-list page outside the page.

I wonder if it is possible to put the resources inside the tile user-list?

hguser
  • 35,079
  • 54
  • 159
  • 293
  • No, because "header" is rendered after both classic.jsp and each of the "res" pages are. – mck Apr 25 '15 at 14:42
  • Then how do you handle this kind of use case if we need to add some inline resources inside the `head` tag? – hguser Apr 27 '15 at 12:34
  • it must be defined before the header attribute is rendered. this usually happens in the controller layer, or by compositing the header attribute instead of relying upon re-using the compositing of the res pages. (you could for example build the "res" list up in the header composition, so to avoid the double attribute-list complexity) – mck Apr 29 '15 at 19:14

1 Answers1

0

If I understand the question correctly -the heart of the problem is that the put-list-attribute value is treated as a String. Which means, we can't use the tiles:insertAttribute tag. I've done it by using tiles:importAttribute at the top, and then loop through the list via the c tags and expression language (el).

Also See Tiles 3 how to reference another definition in put-attribute

baseLayout.jsp

note the link and script tags within the c:forEach

<%@ taglib prefix="c"       uri="http://java.sun.com/jsp/jstl/core"%>
<%@ taglib prefix="tiles"   uri="http://tiles.apache.org/tags-tiles" %>
<%@ taglib prefix="tilesx"  uri="http://tiles.apache.org/tags-tiles-extras" %>
<tiles:importAttribute name="styles" />
<tiles:importAttribute name="scripts" />
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1" >
        <title><tiles:getAsString name="title" /></title>
        <c:forEach items="${styles}" var="style" >
            <link rel="stylesheet" type="text/css" href="<c:url value='${style}' />" />
        </c:forEach>
    </head>
    <body>
        <header id="header">
            <tiles:insertAttribute name="header" />
        </header>
        <section id="body">
            <tiles:insertAttribute name="body" />
        </section>
        <footer id="footer">
            <tiles:insertAttribute name="footer" />
        </footer>
        <c:forEach items="${scripts}" var="script" >
            <script src="<c:url value="${script}"/>"></script>
        </c:forEach>
    </body>
</html>
Community
  • 1
  • 1
Tony Card
  • 2,084
  • 3
  • 20
  • 27