JSP-EL会话变量访问错误:javax.el.PropertyNotFoundException尽管所述属性是公共的 [英] JSP-EL session variable access error: javax.el.PropertyNotFoundException despite said property being public

查看:168
本文介绍了JSP-EL会话变量访问错误:javax.el.PropertyNotFoundException尽管所述属性是公共的的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用MySQL,JDBC,Netbeans创建一个简单的数据库Web应用程序。

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

我在.jsp页面中有以下代码

I have the following code in a .jsp page

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

其中sessionScope.Staff包含Staff Data类型的对象:

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

我提前设置会话对象:

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

我收到以下错误:

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

在会话中的人员数据对象中,可以访问诸如getName()之类的公共方法,但是诸如financial_staff_status之类的公共成员不能访问。

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.

推荐答案

实际上EL表达式存在三个问题:

You actually have three problems with the EL expression:

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




  1. 要评估的条件表达式应在强制性<$ c范围内$ c> test 属性

  2. 属性 status 应作为员工访问.status 因为它已经有一个公共getter方法

  3. 属性 financial_staff_status 需要一个公共的getter方法class StaffData。 EL严格遵守对象类的javabeans合规性以及如何访问属性(必须通过公共getter)。

  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)

此外,它不严格必须限定属性的范围,除非您在不同的范围内具有多个具有相同名称的属性,或者为了清楚起见而希望明确。将从最窄的( pageScope )到最宽的( applicationScope )搜索不同的范围。

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).

financial_staff_status 属性的公共getter添加到您的类后,表达式应为:

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

或简单地说:

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

这篇关于JSP-EL会话变量访问错误:javax.el.PropertyNotFoundException尽管所述属性是公共的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆