java if语句不一致
|
我在bean中有一个“ 0”语句,当我创建一个测试java类时,该语句似乎可以正常运行,但是当由jsp调用bean时,它不能正常工作。
我的代码,让我向您展示:
一,测试类:
package com.serco.inquire;
import java.util.*;
import java.text.*;
public class TestCollection {
public static void main(String[] args) {
IrCollection myCollection = new IrCollection();
myCollection.setSort(\"none\");
myCollection.setMgrid(\"none\");
int endpoint = myCollection.getSize();
for (int i=0;i<endpoint;i++) {
InquireRecord curRec = myCollection.getCurRecords(i);
Long milis = new Long(curRec.getSubmitDate());
Date theDate = new Date(milis);
Format formatter = new SimpleDateFormat(\"dd MMM yyyy\");
String s = formatter.format(theDate);
System.out.println(\"ID: \" + curRec.getID() + \" | Subject: \" + curRec.getSubject());
}
}
}
它调用的IrCollection类中的代码段:
private void processSort(String datum) {
int LastChar = datum.length()-1;
String colName = datum.substring(0, LastChar);
if (datum==\"none\") {
this.fullSort = \" ORDER BY lastUpdated DESC\";
} else {
if (datum.endsWith(\"2\")) {
this.fullSort = \" ORDER BY \" + colName + \" ASC\";
} else {
this.fullSort = \" ORDER BY \" + colName + \" DESC\";
}
}
}
另一个方法中还有更多代码可使用以下方式调用此特定方法:
this.processSort(this.sort);
但是问题是第二个代码样本中的“ 4”部分。假定第一类的第10行将成员变量sort设置为\"none\"
,则processSort()
方法应将成员变量fullSort
设置为\" ORDER BY lastUpdated DESC\"
。
如果我在第一个示例中使用该类,它就会这样做。
然而
我有这个自定义标签:
<%@ tag body-content=\"scriptless\" import=\"com.serco.inquire.*\" %>
<%@ taglib prefix=\"c\" uri=\"http://java.sun.com/jsp/jstl/core\" %>
<%@ attribute name=\"mgr\" required=\"true\" %>
<%@ attribute name=\"mkind\" required=\"false\" %>
<%@ attribute name=\"sort\" required=\"false\" %>
<c:if test=\"${empty mkind}\">
<c:set var=\"mkind\" value=\"manager\" />
</c:if>
<c:if test=\"${empty sort}\">
<c:set var=\"sort\" value=\"none\" />
</c:if>
<jsp:useBean id=\"irc\" scope=\"page\" class=\"com.serco.inquire.IrCollection\">
<jsp:setProperty name=\"irc\" property=\"mgrtype\" value=\"${mkind}\" />
<jsp:setProperty name=\"irc\" property=\"sort\" value=\"${sort}\" />
<jsp:setProperty name=\"irc\" property=\"mgrid\" value=\"${mgr}\" />
</jsp:useBean>
${irc.fullsort}
.jsp文件通过以下哪个调用:
<c:set var=\"user\" value=\"none\" />
<c:set var=\"sort\" value=\"none\" />
<inq:displayCollection>
<jsp:attribute name=\"mgr\">${user}</jsp:attribute>
<jsp:attribute name=\"mkind\">cotr</jsp:attribute>
<jsp:attribute name=\"sort\">${sort}</jsp:attribute>
</inq:displayCollection>
换句话说,将完全相同的数据馈送到IrCollection bean。所以我应该得到相同的数据,对吗?
除了我得到这个:
WHERE COTR = \'none \'由非DES订购
因此,当Java调用它时,它会认为\"none\" == \"none\"
,而当jsp调用它时,它会认为\"none\" != \"none\"
。
没有找到相关结果
已邀请:
5 个回复
青董据零
而不是
:
请参阅“ .equals和==”之间的区别是什么
淑灯
字符串==运算符仅比较指针位置,而不比较字符串的实际值。因此,它在某些情况下会起作用(如果基准面设置为常量字符串),但在动态创建时则无效。
蜗仓馈
闯舱酮
樊熄忙暖
。您的类的编译器可能会为文字的每个实例创建一个共享字符串(例如\“ none \”)。这将导致
,如果从类中分配了字符串,它将检查两个变量是否指向同一对象。然后,当您使用JSP页面时,外部类会提供\“ none \”字符串,从而使它成为另一个对象。 这个故事的寓意是使用'equals()\'比较字符串。