简体   繁体   English

尽管上述属性是公共的,但JSP-EL会话变量访问错误:javax.el.PropertyNotFoundException

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

I am trying to create a simple DB web application using MySQL, JDBC, Netbeans. 我正在尝试使用MySQL,JDBC,Netbeans创建一个简单的DB Web应用程序。

I have the following code in a .jsp page 我在.jsp页面中有以下代码

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

where sessionScope.Staff contains an object of Staff Data type: 其中sessionScope.Staff包含一个Staff Data类型的对象:

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. 在会话的人员数据对象中,可以访问诸如getName()之类的公共方法,但不能访问诸如financial_staff_status之类的公共成员。

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. 非最终变量可以轻松访问而不会出现问题。

You actually have three problems with the EL expression: EL表达式实际上存在三个问题:

<c:if ${sessionScope.Staff.getStatus() == sessionScope.Staff.financial_staff_status} >
  1. The conditional expression to be evaluated should be within the mandatory test attribute 要评估的条件表达式应在强制test属性内
  2. The property status should be accessed as Staff.status as it already has a public getter method 属性status应以Staff.status访问,因为它已经具有公共获取方法
  3. The property financial_staff_status requires a public getter method in class StaffData. 属性financial_staff_status需要在StaffData类中使用公共获取方法。 EL is strict with javabeans compliance of object classes and how properties are accessed (must be via a public getter) EL严格遵守对象类的javabeans兼容性以及如何访问属性(必须通过公共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 ). 从最窄的( pageScope )到最宽的( applicationScope ),将搜索不同的范围。

Having added the public getter for the financial_staff_status property to your class, the expression should be: 在类中添加了financial_staff_status属性的公共获取器后,表达式应为:

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

or simply: 或者简单地:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM