2010-12-22 91 views
8

我遇到以下教程JSP tricks to make templating easier?,它使用JSP創建頁面模板(我怎麼錯過了這麼久?!?)。但是,在做了一些搜索之後,我似乎無法弄清楚如何(或者如果有可能)檢查是否設置了JSP片段。檢查JSP片段是否已設置

這裏是什麼,我試圖做一個例子:

我有一個名爲default.tag模板。它具有定義2個JSP屬性如下:

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

然後,在頁面的代碼,我已經設置爲<jsp:invoke fragment="title" />頁面<title>元件。再後來下來的頁面,我有以下幾點:

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

在那裏我有<c:when test="${true}">,我希望能夠以檢查heading片段是爲了顯示它已定,但如果沒有,則默認爲title片段。

回答

11

在做了更多的混亂之後,我會回答我自己的問題。事實證明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> 

這就是我需要做的。好像我只是想讓它比需要的更難!