如何在 if-else 语句中比较字符串?
我想知道是否有人可以给我一些 JSP 中预填充的指针:
所以我尝试根据客户数据库记录中的值预填充一个复选框。如果该字段中有 1,则该复选框将被选中,另一个选项将为 0 - 复选框应为空。
我目前在输入中使用此代码:
<%if (UPDATEDPREFERENCES != null)
{
if (ISNEWSLETTER != "0") {
out.println("checked");
}
}
%>
所以它会是:
<input type='checkbox' class="newsletter" name='temp_ISNEWSLETTER_FIELD' id="temp_ISNEWSLETTER_FIELD" <%if (UPDATEDPREFERENCES != null)
{
if (ISNEWSLETTER != "0") {
out.println("checked");
}
}
%>/>
就目前情况而言,它预填充得很好。但是,如果字段为 0,它也会填充。如果我将其更改为:
if (ISNEWSLETTER == "1")
,则无论是 1 还是 0,复选框都是空的。
我是否遗漏了一些明显的东西?我不是开发人员,我在纽约,我的支持在比利时 - 他们今天都离开了。
I wonder if anyone could give me a pointer with some pre-population in JSP:
So I am attempting to pre-populate a checkbox depending on the value in the customers database record. The checkbox would be checked if there is a 1 in the field, the other option would be a 0 - where the checkbox should be empty.
I am currently using this code within the input:
<%if (UPDATEDPREFERENCES != null)
{
if (ISNEWSLETTER != "0") {
out.println("checked");
}
}
%>
so it would be:
<input type='checkbox' class="newsletter" name='temp_ISNEWSLETTER_FIELD' id="temp_ISNEWSLETTER_FIELD" <%if (UPDATEDPREFERENCES != null)
{
if (ISNEWSLETTER != "0") {
out.println("checked");
}
}
%>/>
As it stands, it pre-populates fine. However, it also populates if the field is 0. If I change it to read:
if (ISNEWSLETTER == "1")
then the checkbox is empty whether it's a 1 or 0.
Am I missing something obvious? I am not a developer, I am in NYC and my support is in Belgium - they've all left for the day.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
字符串 是 对象,而不是 基元。使用
equals()
,而不是==
/!=
。==
/!=
不会检查它们是否包含相同的值,而是检查它们是否指向相同的对象引用。equals()
将检查它们是否包含相同的值。因此,您应该使用
if (ISNEWSLETTER != "0")
而不是if (ISNEWSLETTER == "1")
您应该使用这个问题不相关到 JSP。在普通的 Java 类(这种代码实际上所属的类)中执行此操作时,您会遇到完全相同的问题。
作为替代方案,由于它们代表数字,您也可以将
ISNEWSLETTER
转换为int
。它是一个基元,因此您应该能够按照通常的方式使用==
/!=
。如果需要,您可以使用Integer#parseInt()
。或者,更重要的是,如果它实际上代表一个布尔状态(它只能是true
或false
),那么您应该使用boolean
代替。您可以直接在if
条件中使用它,而不需要进行比较。Strings are objects, not primitives. Use
equals()
, not==
/!=
. The==
/!=
won't check if they contain the same value, but if they point to the same object reference. Theequals()
will check if they contain the same value.So, instead of
if (ISNEWSLETTER != "0")
you should useand instead of
if (ISNEWSLETTER == "1")
you should useThis problem is not related to JSP. You would have exactly the same problem when doing so in a normal Java class (where this kind of code actually belongs).
As an alternative, since they represent numbers, you could also just convert
ISNEWSLETTER
to anint
. It's a primitive, so you should be able to use==
/!=
the usual way. You can if necessary convert aString
to anint
usingInteger#parseInt()
. Or, even more, if it actually represents a boolean state (it can only betrue
orfalse
), then you should rather useboolean
instead. You can just use it straight in theif
condition without any need for comparisons.