我在一个 bean 中有一个 if
语句,当我创建一个测试 java 类时,它似乎处理得很好,但当该 bean 由 jsp 调用时,它就不能正常工作。
我的代码,让我向您展示一下:
一、测试类:
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);
但问题出在第二个代码示例中的 if (datum=="none")
部分。鉴于第一个类的第 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' ORDER BY non DES
所以当Java调用它时,它认为"none"== "none"
,但是当jsp调用它时,它认为"none"!= "none"
.
最佳答案
关于java if 语句不一致,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6458587/