JSTL - 是否存在put-list-attribute的remove-attribute?

时间:2015-05-05 08:37:35

标签: java jstl tiles apache-tiles

我的主要图块定义有以下代码:

<definition name="main" template="/WEB-INF/jsp/templates/main/template.jsp">
    ...
    <put-list-attribute name="jsBase">
        <add-attribute value="basics" />
        <add-attribute value="jquery" />
    </put-list-attribute>
    <put-list-attribute name="jsExtra">
        <add-attribute value="boostrap" />
        <add-attribute value="d3" />
        <add-attribute value="gridster" />
        <add-attribute value="custom" />
    </put-list-attribute>
</definition>

此定义将用于所有页面。 template.jsp看起来像这样:

<%@ taglib uri="http://tiles.apache.org/tags-tiles" prefix="tiles"%>
<%@ taglib uri="http://tiles.apache.org/tags-tiles-extras" prefix="tilesx" %>
...

    <!DOCTYPE html>
    <html xmlns="http://www.w3.org/1999/xhtml" id="html">
    <head>
    ...

    <tilesx:useAttribute id="jsBase" name="jsBase" classname="java.util.List" />
    <c:forEach var="file" items="${jsBase}">
        <script type="text/javascript" src="<c:url value="/js/${file}.js"/>" />
    </c:forEach>

    <tilesx:useAttribute id="jsExtra" name="jsExtra" classname="java.util.List" />
    <c:forEach var="file" items="${jsExtra}">
        <script type="text/javascript" src="<c:url value="/js/${file}.js"/>" />
    </c:forEach>
    ...

我们的想法是加载jsBasejsExtra中定义的所有文件,考虑到我将保持所有页面的jsBase不受影响,但jsExtra可能与页面不同到页面。我知道存在inheratance属性,可以为列表添加额外的值或用新值覆盖它,但我想要的是继承原始列表中的所有值并只删除一个值,所以我不必再次定义我想要保留原始列表的值。可以做这样的事情吗?:

    <put-list-attribute name="jsExtra" inherit="true">
        <remove-attribute value="custom" />
    </put-list-attribute>

如果不能使用类似的东西,可以使用一些解决方法来防止重复的代码?

2 个答案:

答案 0 :(得分:1)

根据需要&#34;继承原始列表中的所有值并删除一个值&#34;,如果脚本文件是您要排除的,则不要在页面中显示它。

<c:forEach var="file" items="${jsExtra}">
    <c:if test="${file != 'custom'}">
        <script type="text/javascript" src="<c:url value="/js/${file}.js"/>" />
    </c:if>
</c:forEach>

答案 1 :(得分:0)

感谢John Lee的回答。但是没有什么问题:每个页面都会加载template.jsp,我想为特定页面排除custom.js,而不是所有页面(如果是这样的话,我就不会包含{{}首先在custom.js中1}}。 但是以你的想法为灵感,我得出了这个解决方案:

jsExtra

快速说明:我定义了一个排除列表,其中包含我不想为我在tile文件中定义的每个页面加载的文件名。这样我就可以从我的主要定义继承<tilesx:useAttribute id="jsExclude" name="jsExclude" classname="java.util.List" /> <tilesx:useAttribute id="jsExtra" name="jsExtra" classname="java.util.List"/> <c:forEach var="file" items="${jsExtra}"> <c:if test="${!jsExclude.contains(file)}"> <script type="text/javascript" src="<c:url value="/js/${file}.js"/>"></script> </c:if> </c:forEach> 中包含的所有值,并选择我想要排除的值。