java 如何用 <c:if> 比较 2 个变量
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7906525/
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
How to compare 2 variables with <c:if>
提问by Sergey
I am using jsp + spring mvc, and on jsp page I have some list - catList
with id
and name
and also have some variable test
. I am trying to compare cat.id
and test
, but can't because every time have runtime syntax errors:
我使用的JSP + Spring MVC和JSP页面上,我有一些名单-catList
与id
和name
,也有一些变化test
。我试图比较cat.id
and test
,但不能,因为每次都有运行时语法错误:
<c:forEach var="cat" items="${catList}" varStatus="i">
<c:out value="${cat.id}"/>
<%-- comparison and some action--%>
</c:forEach>
tried:
试过:
<c:if test="${category.id == test}" >
<c:if test="${category.id eq test}" >
<c:if test="${category.id eq ${test}}">
Update: I resolved this problem, simply it was error with server redeploy
更新:我解决了这个问题,只是服务器重新部署出错
回答by Michael
All JSTL tags (as well as XML and HTML tags) require an opening tagand a closing tag. The opening tag defines where the tag body starts (and also allows you to define attributes). The closing tag defines where the tag body ends.
所有 JSTL 标记(以及 XML 和 HTML 标记)都需要一个开始标记和一个结束标记。开始标签定义了标签正文的开始位置(并且还允许您定义属性)。结束标签定义了标签主体的结束位置。
In the following code sample, the closing tag is on the last line.
在以下代码示例中,结束标记位于最后一行。
<c:if test="${category.id == test}" >
<b>Test passed!</b>
</c:if>
As you can see, it has the same name as the opening tag, and starts with a /
(and has no attributes).
如您所见,它与开始标签同名,并以 a 开头/
(并且没有属性)。
The first two opening tags that you included in your question should work.
您在问题中包含的前两个开始标签应该有效。