0
我正在嘗試使用MySQL,JDBC,Netbeans創建一個簡單的數據庫Web應用程序。JSP-EL會話變量訪問錯誤:javax.el.PropertyNotFoundException儘管所述屬性被公開
我有一個.JSP頁面下面的代碼
<c:if ${sessionScope.Staff.getStatus() == sessionScope.Staff.financial_staff_status} >
Financial staff
</c:if>
其中sessionScope.Staff包含員工數據類型的對象:
public class StaffData
{
//constants
public final byte default_staff_status = 0;
public final byte financial_staff_status = 1;
public final byte legal_staff_status = 2;
public final byte secretarial_staff_status = 3;
//other data
StaffData()
{
//initializations
}
void authenticate(int staff_num, String passwd) throws ClassNotFoundException, SQLException
{
//connect to sever, blah, blah
}
public String getName()
{
return this.name;
}
public int getNumber()
{
return this.staff_number;
}
public byte getStatus()
{
return this.status;
}
}
我設置會話對象事先:
request.getSession().setAttribute("Staff", currentStaff);
我收到以下錯誤:
javax.el.PropertyNotFoundException: Property 'financial_staff_status' not found on type staff.StaffData
在會話中的工作人員數據對象中,可以訪問公共方法(如getName()),但公共成員(如financial_staff_status)不能。
爲什麼我會遇到這個問題?問題似乎與最終的變數有關。非最終變量可以輕鬆訪問而不會出現問題。
感謝。我對測試屬性感到困惑。另外,我想用最後的變量作爲常量。沒有getter方法有點擊敗目的?最後,我想在會議中擁有可以在註銷時刪除的數據。這是實施這個的正確方法嗎? – TSG 2014-11-21 22:09:56
嗨@TSG,感謝您接受答案,很高興幫助。具有原始常量的getters並不是問題,因爲只返回它們的值(對象聲明爲final並返回給客戶端可能仍然會改變其內部狀態)。你應該考慮使這些常量是靜態的。將對象存儲爲會話屬性很好;你可以添加一個會話監聽器來在會話被銷燬時手動清理對象 - 但我們現在要開始討論話題了! – 2014-11-23 16:25:08