0

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.

0

1 Answer 1

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}">
2
  • Thanks. I was confused about the test attribute. Also, I wanted to use the final variables as constants. doesn't having a getter method kinda defeat that purpose? Finally, I want to have the data in a session that I can remove at logout. Is this the correct way to implement this?
    – TSG
    Nov 21, 2014 at 22:09
  • Hi @TSG, thanks for accepting the answer, glad it helped. Having getters for primitive constants is not a problem, as only their value is returned (Objects declared as final and returned to clients may still have their internal state changed). You should consider making those constants static, too. Storing the object as a session attribute is fine; you could add a session listener to manually cleanup objects when the session is destroyed - but we're going off topic now! Nov 23, 2014 at 16:25

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.