JSP-EL session variable access error: javax.el.Pro

2019-09-03 13:03发布

问题:

I am trying to create a simple DB web application using MySQL, JDBC, Netbeans.

I have the following code in a .jsp page

<c:if ${sessionScope.Staff.getStatus() == sessionScope.Staff.financial_staff_status} >
    Financial staff
</c:if> 

where sessionScope.Staff contains an object of Staff Data type:

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;
    }
}

I am setting the session object in advance:

request.getSession().setAttribute("Staff", currentStaff);

I am getting the following error:

javax.el.PropertyNotFoundException: Property 'financial_staff_status' not found on type staff.StaffData

In the staff data object in the session, public methods such as getName() can be accessed, but public members such as financial_staff_status cannot.

Why am I getting this problem? The problem seems to be with the final variables. Non final variables can easily be accessed without a problem.

回答1:

You actually have three problems with the EL expression:

<c:if ${sessionScope.Staff.getStatus() == sessionScope.Staff.financial_staff_status} >
  1. The conditional expression to be evaluated should be within the mandatory test attribute
  2. The property status should be accessed as Staff.status as it already has a public getter method
  3. The property financial_staff_status requires a public getter method in class StaffData. EL is strict with javabeans compliance of object classes and how properties are accessed (must be via a public getter)

Additionally, its not strictly necessary to qualify the scope of an attribute, unless you have multiple attributes with the same name in different scopes or wish to be explicit for clarity. The different scopes will be searched starting with the narrowest (pageScope) through to the widest (applicationScope).

Having added the public getter for the financial_staff_status property to your class, the expression should be:

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

or simply:

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