This question already has an answer here:
这个问题在这里已有答案:
- Comparing numbers in EL expression does not seem to work 2 answers
比较EL表达式中的数字似乎不起作用2个答案
I'm having a strange behavior since our upgrade from Tomcat 6 to Tomcat 8.0.32.
自从我们从Tomcat 6升级到Tomcat 8.0.32以来,我有一种奇怪的行为。
Relational operators (<, >, <=, >=) are not working with variables defined with c:set
关系运算符(<,>,<=,> =)不适用于使用c:set定义的变量
public class ServiceConstants {
public static final Integer MY_CONST = 15;
}
Below is my code (updated):
以下是我的代码(更新):
<%@ page isELIgnored="false"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<c:set var="a" value="<%=ServiceConstants.MY_CONST%>"/>
<c:set var="b" value="${127}" />
<html>
<body>
<br/>a: ${a}
<br/>b: ${b}
<br/>Using variables {b > a}: ${b > a}
<br/>Using variables {b gt a}: ${b gt a}
<br/>Hardcoded values {127 > 15}: ${127 > 15}
</body>
</html>
And below is what is rendered
以下是渲染的内容
a: 15
b: 127
Using variables {b > a}: false
Using variables {b gt a}: false
Hardcoded values {127 > 15}: true
When comparing a and b set by c:set it is returning the wrong answer.
当比较由c:set设置的a和b时,它返回错误的答案。
Below is my web.xml file
下面是我的web.xml文件
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
version="3.0" metadata-complete="true">
<absolute-ordering />
<distributable/>
<display-name>App name</display-name>
<jsp-config>
<taglib>
<taglib-uri>http://xyzo.org/app</taglib-uri>
<taglib-location>/WEB-INF/tld/app.tld</taglib-location>
</taglib>
</jsp-config>
</web-app>
I tried several solutions posted, changing to Tomcat 8.0.37 (latest), change my web.xml file header and such.
我尝试了几个解决方案,更改为Tomcat 8.0.37(最新),更改我的web.xml文件头等。
Any help is much appreciated.
任何帮助深表感谢。
EDIT BELOW:
I've found out that the numbers are being interpreted as strings by EL. If I force cohersion it works but seems cumbersome.
我发现这些数字被EL解释为字符串。如果我强迫凝聚它有效,但似乎很麻烦。
<%@ page isELIgnored="false"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<c:set var="a" value="<%=ServiceConstants.MY_CONST%>"/>
<c:set var="b" value="${127}" />
<html>
<body>
<br/>a: ${a}
<br/>b: ${b}
<br/>Using variables {b > a}: ${b > a}
<br/>Using variables {b gt a}: ${b gt a}
<br/>Hardcoded values {127 > 15}: ${127 > 15}
<br/>...
<br/>Forcing cohersion
<br/>Using variables {b > (0 + a)}: ${b > (0 + a)}
</body>
</html>
It results:
a: 15
b: 127
Using variables {b > a}: false
Using variables {b gt a}: false
Hardcoded values {127 > 15}: true
...
Forcing cohersion
Using variables {b > (0 + a)}: true
Any tips on how to make EL to do the 'right thing' still would be appreciated.
任何有关如何让EL做“正确的事情”的提示仍然会受到赞赏。
1 个解决方案
#1
1
If you use expressions when setting the variables it's less cumbersome:
如果在设置变量时使用表达式,则不那么麻烦:
<c:set var="a" value="${15}"/>
<c:set var="b" value="${127}" />
#1
1
If you use expressions when setting the variables it's less cumbersome:
如果在设置变量时使用表达式,则不那么麻烦:
<c:set var="a" value="${15}"/>
<c:set var="b" value="${127}" />