2011-07-14 55 views
1

我有一個非常簡單的自定義JSP標記,用於生成分頁鏈接。它去大致如下:JSP/JSTL:'2> 10'評估爲true

<span id="${id}" class="paginationLinks ${cssClass}"> 
    <c:if test="${currentPage gt 1}"> 
     <!-- Links to previous page(s) --> 
    </c:if> 
    <span class="paginationCurrentPage"> 
     Page ${currentPage} 
     [DEBUG: current=${currentPage}, 
       total=${totalPages}, 
       show=${currentPage lt totalPages} 
       inverse=${currentPage gt totalPages}] 
    </span> 
    <c:if test="${currentPage lt totalPages}"> 
     <!-- Links to next page(s) --> 
    </c:if> 
</span> 

的問題是,鏈接,進入下一個頁面無法顯示的第一頁(currentPage = 1)之後的。轉到上一頁的鏈接在每個頁面上都能正常工作。我也得到了一些真正怪異的輸出從我的調試塊:

[DEBUG: current=1, total=10, show=true inverse=false] //first page, correct 
[DEBUG: current=2, total=10, show=false inverse=true] //second page; 2 > 10 == true? wtf??? 
[DEBUG: current=9, total=10, show=false inverse=true] //ninth page, still incorrect 
[DEBUG: current=10, total=10, show=false inverse=false] //tenth page, correct 

兩個currentPagetotalPageslong類型的請求屬性,並通過申報標籤屬性傳遞給標籤。那麼,我做錯了什麼產生如2 > 10 == true這樣的瘋狂輸出?

更新

它正常工作,如果我用在比較文字10更換totalPages,但真的沒有解決不了的問題。

回答

1

找到解決方案。我需要顯式聲明對我的標籤類型的屬性,如:

<%@ attribute name="currentPage" required="true" type="java.lang.Long" %> 
<%@ attribute name="totalPages" required="true" type="java.lang.Long" %> 

我懷疑,如果沒有聲明的類型兩個屬性都被解釋爲字符串,並且標籤在做的字符串值之間的逐一比較數字。我假設10的字面值正常工作,因爲JSP解釋器將其識別爲適當的數字類型,然後自動轉換比較中的其他參數以匹配。

這麼久以來,總是在您的標籤屬性上聲明type。否則會發生令人困惑的事情。

+0

回答你自己的問題的好工作。 –

相關問題