【问题标题】:Check if JSP fragment has been set检查是否已设置 JSP 片段
【发布时间】:2011-05-29 11:19:03
【问题描述】:

我遇到了以下教程JSP tricks to make templating easier? 使用 JSP 创建页面模板(我怎么错过了这么久?!?)。但是,在进行了一些搜索之后,我似乎无法弄清楚如何(或者是否可以)检查是否已设置 JSP 片段。

这是我正在尝试做的一个示例:

我有一个名为default.tag 的模板。它有 2 个 JSP 属性,定义如下:

<%@attribute name="title" fragment="true" required="true" %>
<%@attribute name="heading" fragment="true" %>

然后在页面的代码中,我将页面的&lt;title&gt; 元素设置为&lt;jsp:invoke fragment="title" /&gt;。然后在页面下方,我有以下内容:

<c:choose>
    <c:when test="${true}">
        <jsp:invoke fragment="heading" />
    </c:when>
    <c:otherwise>
        <jsp:invoke fragment="title" />
    </c:otherwise>
</c:choose>

在我有&lt;c:when test="${true}"&gt; 的地方,我希望能够检查是否设置了heading 片段以便显示它,但如果没有,则默认为title 片段。

【问题讨论】:

    标签: jsp


    【解决方案1】:

    在做了更多的混乱之后,我将回答我自己的问题。事实证明,attribute 的名称实际上也变成了一个变量。因此,我可以做到以下几点:

    <c:choose>
        <c:when test="${not empty heading}">
            <jsp:invoke fragment="heading" />
        </c:when>
        <c:otherwise>
            <jsp:invoke fragment="title" />
        </c:otherwise>
    </c:choose>
    

    这就是我需要做的。好像我只是想让它变得比需要的更难!

    【讨论】:

    • 提一下,不要使用header作为名字,如果没有提供fragment,那么属性会保存请求头
    猜你喜欢
    • 2020-03-12
    • 2017-10-25
    • 1970-01-01
    • 2014-05-22
    • 2013-09-29
    • 2014-06-15
    • 1970-01-01
    • 2011-07-14
    • 2018-12-18
    相关资源
    最近更新 更多