【问题标题】:JSP/JSTL: '2 > 10' Evaluates to trueJSP/JSTL: '2 > 10' 评估为真
【发布时间】:2011-07-14 04:55:06
【问题描述】:

我有一个非常简单的自定义 JSP 标记,用于生成分页链接。大致如下:

<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

currentPagetotalPages 都是long 类型的请求属性,并通过声明的标记属性传递给标记。那么我做错了什么来产生像2 &gt; 10 == true这样的疯狂输出?

更新

如果我在比较中将totalPages 替换为文字10,它可以正常工作,但这确实不能解决问题。

【问题讨论】:

    标签: jsp jstl jsp-tags


    【解决方案1】:

    找到解决方案。我需要在标签属性上显式声明类型,例如:

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

    我怀疑如果没有声明的类型,两个属性都被解释为字符串,并且标签正在数字的字符串值之间进行字典比较。我假设 10 的文字值有效,因为 JSP 解释器将其识别为正确的数字类型,然后自动将比较中的另一个参数转换为匹配。

    长话短说,请始终在您的标签属性上声明type。否则会发生非常混乱的事情。

    【讨论】:

      猜你喜欢
      • 2016-08-25
      • 1970-01-01
      • 2021-11-03
      • 2017-12-18
      • 2014-05-19
      • 2015-12-23
      • 1970-01-01
      • 2016-07-10
      相关资源
      最近更新 更多