Reputation: 13485
I have this JSP EL expression, which uses the >= comparator. On my development environment I get the result you would expect, namely that 2 >= 188 is false. However, on my staging and production servers apparently 2 >= 188 is true.
Here is the code:
</ul>
<p>curPage: ${param.curPage}<br/>
totalPages: ${param.totalPages}<br/>
totalPages - curPage: ${param.totalPages - param.curPage}<br/>
curPage gt totalPages: ${param.curPage >= param.totalPages}<br/>
<p>
On my development environment, I get output like this:
curPage: 2
totalPages: 68
totalPages - curPage: 66
curPage gt totalPages: false
On staging:
curPage: 2
totalPages: 181
totalPages - curPage: 179
curPage gt totalPages: true
My development environment is running Tomcat 7.0.29, staging is running 7.0.30. The codebases are the same.
The above code is in a file "pagination.jsp" (I know, it should be a .tag), which is included into another jsp like this:
<jsp:include page="/widgets/pagination.jsp">
<jsp:param name="totalPages" value="${actionBean.jbp.totalPages}" />
<jsp:param name="baseUrl" value="${baseUrl}" />
<jsp:param name="curPage" value="${not empty param.page?param.page:0}" />
</jsp:include>
"jbp.totalPages" is defined as:
private final int totalPages;
and "param.page" is obviously a page parameter.
I suppose there could be a type conversion problem here, between the parameter and the int, but that does not explain why it works on one machine and not the other.
Also, I thought JSP EL did automatic type conversion.
Any thoughts would be appreciated.
Upvotes: 2
Views: 147
Reputation: 88468
2 >= 188
is false if the items compared are numbers.
2 >= 188
is true if the items compared are strings.
You have something different in your two deployments.
Look at the versions of jstl.jar and standard.jar and all the jars, perhaps, in your tomcat/lib
directories. Maybe your code is the same but there are other differences in dependent libraries? Somehow the types are different. It would be unlikely, but not beyond the realm of possibility, that differences in JSP are effecting type inference.
EDIT:
This was answered back when the question used 188 (actually 181) for both code examples. It was since changed to 69 in one of the examples. Now it all makes sense.
Upvotes: 3
Reputation: 1109665
Those params are evaluated as String
instead of Number
(because that's basically what HttpServletRequest#getParameter()
returns). A string value of "2"
is lexicographically "greater" than any string value starting with "1"
.
You need to parse them as Number
. You can use JSTL <fmt:parseNumber>
for this.
<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %>
...
<fmt:parseNumber var="curPage" value="${param.curPage}" integerOnly="true" />
<fmt:parseNumber var="totalPages" value="${param.totalPages}" integerOnly="true" />
...
<p>
curPage: ${curPage}<br/>
totalPages: ${totalPages}<br/>
totalPages - curPage: ${totalPages - curPage}<br/>
curPage gt totalPages: ${curPage >= totalPages}<br/>
</p>
As to the difference in development versus production environment, this is most likely result of bad testing (not using same test data or maybe even not same test code). There is namely nothing like that changed in Tomcat's EL implementation across the mentioned versions. Only if one of the hands in the comparison is really a Number
and the other is a String
, then EL will coerce the String
to Long
. This is perhaps what is happening in real code.
Upvotes: 4