java if 语句不一致

标签 java jsp jstl javabeans

我在一个 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" .

最佳答案

您必须使用equals而不是==:

datum.equals("无")

引用whats the difference between “.equals and ==”

关于java if 语句不一致,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6458587/

相关文章:

java - 调试 Apache Tomcat

java - 抛出 ClassCast 异常

jsp - 未知标签 (c :if) warning in jsp

java - 如何在迭代 <c :foreach> tag in select tag in JSP? 时从任何列表对象中删除元素

java - 关于 web.xml 中的错误页面

jsp 上的 javax.el.PropertyNotFoundException 错误

java - 无法在Idea中构建项目

java - 使用其他东西代替字符串

javascript - 使用 javascript 显示过去 3 个月的下拉列表

html - 在 firefox、chrome 中处理像 css 这样的 jsp 的问题