JSP/JSTL:"2> 10'评估为真 [英] JSP/JSTL: '2 > 10' Evaluates to true

查看:51
本文介绍了JSP/JSTL:"2> 10'评估为真的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个非常简单的自定义JSP标记,用于生成分页链接.大概是这样的:

I have a very simple custom JSP tag that I am using to generate pagination links. It goes roughly like:

<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)之后.转到上一页的链接在每个页面上均正常工作.我还从调试块中得到了一些真正奇怪的输出:

The problem is that the links to go to the next page are not showing up after the first page (currentPage = 1). The links to go to previous pages are working correctly across every page. I'm also getting some truly bizarre output from my debug block:

[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 > 10 == true这样的疯狂输出我做了什么错误的事情?

Both currentPage and totalPages are request attributes of type long and are passed to the tag via declared tag attributes. So what have I done wrong to produce such insane output as 2 > 10 == true?

更新

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

It works correctly if I replace totalPages with a literal 10 in the comparison, but that really does not solve the problem.

推荐答案

找到解决方案.我需要在标记属性中明确声明类型,例如:

Solution found. I needed to explicitly declare the type on my tag attributes, like:

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

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

I suspect that without the declared type both attributes were being interpreted as Strings, and the tag was doing a lexicographical comparison between the string values of the numbers. I assume a literal value of 10 worked because the JSP interpreter recognized it as a proper numerical type and then automatically converted the other argument in the comparison to match.

长话短说,请始终在标签属性上声明type.否则会发生令人困惑的事情.

So long story short, always declare a type on your tag attributes. Otherwise very confusing things can happen.

这篇关于JSP/JSTL:"2&gt; 10'评估为真的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆