JSTL - 是否存在用于放置列表属性的删除属性?

JSTL - Exists the remove-attribute for put-list-attribute?

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

<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 可能会有所不同从一页到另一页。我知道存在可以向列表添加额外值或用新值覆盖它的继承 属性 ,但我想要的是继承原始列表中的所有值并只删除一个值,所以我不这样做必须再次定义我想从原始列表中保留的值。可以做这样的事情吗?:

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

如果无法使用类似的东西,是否有一些解决方法来防止重复代码?

根据需要"inherit all values from the original list AND remove just one value ",如果脚本文件是你想要排除的,那么就不要在页面中显示它。

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

感谢 John Lee 的回答。但是没有什么问题:每个页面都加载了 template.jsp,我想排除特定页面的 custom.js,而不是所有页面(如果是这样,我就不会包括 custom.jsjsExtra 中排在首位)。 但以您的想法为灵感,我想到了这个解决方案:

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

快速解释:我定义了一个排除列表,其中包含我不想为我在 tiles 文件中定义的每个页面加载的文件名。这样我就可以从我的主要定义中继承 jsExtra 中包含的所有值,并继承 select 我想排除的值。