2014-11-21 33 views
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)不能。

爲什麼我會遇到這個問題?問題似乎與最終的變數有關。非最終變量可以輕鬆訪問而不會出現問題。

回答

1

你實際上有三個問題與EL表達式:

<c:if ${sessionScope.Staff.getStatus() == sessionScope.Staff.financial_staff_status} > 
  1. 條件表達式進行評估應該是強制性的test屬性
  2. 酒店statusStaff.status已被訪問,因爲它內有一個公共的getter方法
  3. 屬性financial_staff_status需要類StaffData中的公共getter方法。 EL嚴格遵守javabeans的對象類以及如何訪問屬性(必須通過公共getter)

此外,它不是必須限定屬性的範圍,除非您具有相同的多個屬性不同範圍的名稱或希望明確說明。不同的範圍將從最窄(pageScope)到最寬(applicationScope)開始搜索。

已經添加了financial_staff_status屬性類的公共的getter,表達應該是:

<c:if test="${sessionScope.Staff.status == sessionScope.Staff.financial_staff_status}"> 

或者乾脆:

<c:if test="${Staff.status == Staff.financial_staff_status}"> 
+0

感謝。我對測試屬性感到困惑。另外,我想用最後的變量作爲常量。沒有getter方法有點擊敗目的?最後,我想在會議中擁有可以在註銷時刪除的數據。這是實施這個的正確方法嗎? – TSG 2014-11-21 22:09:56

+0

嗨@TSG,感謝您接受答案,很高興幫助。具有原始常量的getters並不是問題,因爲只返回它們的值(對象聲明爲final並返回給客戶端可能仍然會改變其內部狀態)。你應該考慮使這些常量是靜態的。將對象存儲爲會話屬性很好;你可以添加一個會話監聽器來在會話被銷燬時手動清理對象 - 但我們現在要開始討論話題了! – 2014-11-23 16:25:08