gpt4 book ai didi

java - condition in jsp executes all conditions

转载 作者:行者123 更新时间:2023-11-29 06:32:29 28 4
gpt4 key购买 nike

我正在尝试在 Netbeans 中制作一个简单的 MySQL、Java JDBC Web 应用程序。我希望根据当前 session 中的状态变量显示不同的内容。我尝试了以下方法:

首先,我在 .jsp 页面中有以下代码:

<c:choose>
<c:when test="${sessionScope.Staff.getStatus() == staff.default_staff_status}">Default staff</c:when>
<c:when test="${sessionScope.Staff.getStatus() == staff.financial_staff_status}">Financial staff</c:when>
<c:when test="${sessionScope.Staff.getStatus() == staff.legal_staff_status}">Legal staff</c:when>
<c:otherwise>Secretarial staff</c:otherwise>
</c:choose>

其次,我在 .jsp 页面中有以下代码:

<c:if test = "${sessionScope.Staff.getStatus() == Staff.default_staff_status}" >
Default staff
</c:if>
<c:if test = "${sessionScope.Staff.getStatus() == Staff.financial_staff_status}" >
Financial staff
</c:if>
<c:if test = "${sessionScope.Staff.getStatus() == Staff.legal_staff_status}" >
Legal staff
</c:if>
<c:if test = "${sessionScope.Staff.getStatus() == Staff.secretarial_staff_status}" >
Secretarial staff
</c:if>

sessionScope.Staff 给出一个对象 StaffData 定义为:

public class StaffData 
{
protected final byte default_staff_status = 0;
protected final byte financial_staff_status = 1;
protected final byte legal_staff_status = 2;
protected final byte secretarial_staff_status = 3;

private byte status;
//Other data

StaffData()
{
//Constructor
}

//Other methods

public byte getStatus()
{
return this.status;
}

public byte getDefault_staff_status()
{
return this.default_staff_status;
}

public byte getFinancial_staff_status()
{
return this.financial_staff_status;
}

public byte getLegal_staff_status()
{
return this.legal_staff_status;
}

public byte getSecretarial_staff_status()
{
return this.secretarial_staff_status;
}
}

通过这两种方法,我的输出是:

    Default staff Financial staff Legal staff Secretarial staff

但是,应该只打印其中一个。所有的 getter 函数都是公开的并且定义正确。为什么我看到所有的行都在打印?

相关的早期问题:JSP-EL session variable access error: javax.el.PropertyNotFoundException despite said property being public

最佳答案

我发现了我的错误。我忘了包括:

<%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>

不知道为什么我没有得到任何错误

关于java - <c : choose> condition in jsp executes all conditions,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27071904/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com