Java 用 JSTL 比较字符串

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2254806/
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-08-13 05:17:47  来源:igfitidea点击:

Comparing strings with JSTL

javajsfjstl

提问by Moon13

I have two strings that i need to compare, but even if they have the same values or different , it alwaysenters the statement...

我有两个需要比较的字符串,但即使它们具有相同或不同的值,它也总是进入语句...

<c:when test="#{bean.name1 != bean.name2}">
     fields that are supposed to appear _only_ when name1 is different from name2
</c:when>

采纳答案by John Vint

The problem is that you probably did not wrap the when in a choose tag.

问题是您可能没有将 when 包装在选择标签中。

if you have:

如果你有:

    <c:choose>
    <c:when test="${bean.name1 != bean.name2}">
        fields that are supposed to appear _only_ when name1 is different from name2
    </c:when>
</c:choose>

It will work

它会工作

回答by Zinc

Try this...

尝试这个...

<c:if test="${bean.name1 ne bean.name2}">
     fields that are supposed to appear _only_ when name1 is different from name2
</c:if>

ne = not equal

ne = 不相等

Also

# should be $

# 应该是 $

回答by fastcodejava

Should it be fields that are supposed to appear onlywhen name1 is different from name2

是否应该是在 name1 与 name2 不同 时才出现的字段

回答by fastcodejava

Should it be ?

应该是吗?

<c:if test="#{bean.name1 != bean.name2}">
     // code
</c:if>

EDIT : <c:when>is supposed to be inside <c:choose>. Cannot ask why, that is just the syntax. It is like asking why ifwill not work in place of switchin C/C++/Java. They are just different animals.

编辑: <c:when>应该在里面<c:choose>。不能问为什么,这只是语法。这就像问为什么if不能代替switchC/C++/Java 一样工作。他们只是不同的动物。

回答by u7867

Does it make any difference if you do this:

如果你这样做有什么区别吗:

<c:when test="${bean.name1 != bean.name2}">
     fields that are supposed to appear _only_ when name1 is different from name2
</c:when>

回答by GreenieMeanie

I have noticed some flakiness when using c:if or c:choose and c:when inside some jsf iteration components, such as rich:datatable. What is the full context?

我注意到在某些 jsf 迭代组件(例如 Rich:datatable)中使用 c:if 或 c:choose 和 c:when 时有些不稳定。完整的上下文是什么?

As a workaround, I would commonly have to wrap things in an a4j:outputPanel and set the rendered attribute accordingly.

作为一种解决方法,我通常必须将内容包装在 a4j:outputPanel 中并相应地设置呈现的属性。