java 如何比较JSTL中的参数

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5563025/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-30 11:40:42  来源:igfitidea点击:

How to compare parameters in JSTL

javajspjstlel

提问by Moham Qmaizer

I have list of ID's from a database and pass them in a request to a servlet, but i can't compare them to any number.

我有来自数据库的 ID 列表,并将它们在请求中传递给 servlet,但我无法将它们与任何数字进行比较。

I think maybe i have to convert them to integer type:

我想也许我必须将它们转换为整数类型:

    <c:forEach items="${subjec.id}" var="x">
${x}
  </c:forEach>
<c:forEach items="${listPage}" var = "list">
${list.id} 
<c:choose>
<c:when test="${list.id} == 1">

</c:when>
</c:choose>

</c:forEach>
<c:if test="${subject1.id == 1}"> 
${subject1.id}
</c:if>

However, I cant test based on Integer format. Any suggetions for this case?

但是,我无法基于整数格式进行测试。对这种情况有什么建议吗?

回答by BalusC

<c:when test="${list.id} == 1">

This is wrong. You need to put the entireexpression inside ${}.

这是错误的。您需要将整个表达式放入${}.

If getId()returns a Number(integer, long, etc), then use the following:

如果getId()返回Number(整数、长整数等),则使用以下命令:

<c:when test="${list.id == 1}">

Or if it returns a String(which is unnatural by the way), then use the following:

或者,如果它返回 a String(顺便说一下,这是不自然的),则使用以下内容:

<c:when test="${list.id == '1'}">

Or if it returns a boolean(just as an example), then use the following:

或者,如果它返回一个boolean(仅作为示例),则使用以下内容:

<c:when test="${list.id}">

回答by Abhinab Kanrar

it should be <c:when test="${list.id == '1'}">
if you use <c:when test='${list.id == "1"}'>exception will be thrown

应该是<c:when test="${list.id == '1'}">
如果你使用<c:when test='${list.id == "1"}'>异常会被抛出

回答by krackmoe

try

尝试

<c:when test='${list.id == "1"}'>