处理 Apache 磁贴中的静态资源



我正在使用 apache tiles3,我有一个带有默认空属性的经典布局:

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

作为shon,有一个名为res的属性要插入,这意味着静态javascript或css或内联样式。

对于 javascript,我可以直接将它们添加到磁贴中,但对于 css 或内联样式,它们可以添加到 head 部分中,这就是我添加res占位符的原因。

现在我有一个可能使用引导和主干库的布局,所以我这样定义它:

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

然后对于一个 conrete 页面,我将放置所有必需的属性,以及它自己的资源(如果有):

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

请参阅jsps:https://i.stack.imgur.com/qYAdM.png

它可以工作,但我发现这很不方便,因为我必须将user-list页面的资源放在页面之外。

我想知道是否可以将资源放在瓷砖user-list内?

如果我正确理解了这个问题 - 问题的核心是 put-list-attribute 被视为字符串。 这意味着,我们不能使用 tiles:insertAttribute 标签。 我已经通过使用顶部的tiles:importAttribute来完成,然后通过c标签和表达式语言(el)循环浏览列表。

另请参阅磁贴 3 如何在放置属性中引用另一个定义

基本布局.jsp

记下 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>

最新更新