JSP-EL session variable access error: javax.el.PropertyNotFoundException despite said property being public

拈花ヽ惹草 提交于 2019-12-13 04:55:04

问题


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


来源:https://stackoverflow.com/questions/27054792/jsp-el-session-variable-access-error-javax-el-propertynotfoundexception-despite

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!