简体   繁体   English

如何比较JSP中的两个变量

[英]How to compare two variables in jsp

I am using the following code to compare two variables, but it seems it has an error as the page does not get loaded at all. 我正在使用以下代码比较两个变量,但由于页面根本没有加载,因此似乎有错误。

Based on this answer my code should be correct but surprisingly does not work. 根据这个答案,我的代码应该是正确的,但是令人惊讶的是它不起作用。

If I remove the <c:when> line it shows the values of ${name} and ${myvar.employee.name} . 如果删除<c:when>行,它将显示${name}${myvar.employee.name}

<c:forEach var="myvar" items="${user.names}">

    ${name}
    ${myvar.employee.name}

    <c:when test="${name eq myvar.employee.name}">
        hello
    </c:when>

The structure with <c:when> requires <c:choose> <c:when>的结构需要<c:choose>

<c:choose>
   <c:when test="${your condition here}">
   </c:when>
   <c:otherwise>
   </c:otherwise>
</c:choose>

otherwise you will get error. 否则您将得到错误。

As Alex has pointed out, you cannot have non-wrapped <c:when> in your code. 正如Alex所指出的,您的代码中不能包含未包装的<c:when>

But it looks like, in your case, <c:choose> is not required, because you have only a single clause. 但是对于您来说,看起来<c:choose>不是必需的,因为您只有一个子句。 In this case <c:if> may be a better option: 在这种情况下, <c:if>可能是一个更好的选择:

<c:if test="${name eq myvar.employee.name}">
  hello
</c:if>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM