2011-07-14 42 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

回答你自己的问题的好工作。 –

相关问题