Bean的ArrayList ..如何在jsp中使用EL访问bean的属性?引发NotFoundError异常

时间:2014-05-06 10:31:31

标签: java jsp arraylist jstl javabeans

我在请求范围中设置了employeebean resultEmployeeList的arraylist。 我不知道我是不是以正确的方式做到了..但这里是jsp中使用的jstl代码..

<c:forEach var="element" items="${resultEmployeeList} ">
            <tr>

                <td>
                ${element.empId}
                </td>
                <td> ${element.empname}</td>


            </tr>
        </c:forEach>

当我尝试访问employeebean的empId属性时,它显示此错误

javax.el.PropertyNotFoundException: Property 'empid' not found on type java.lang.String

这是employeebean

public class EmployeeBean {

private int empId;
private String empname;
private boolean exceptionExist;
public EmployeeBean() {
}

public int getEmpId() {
    return empId;
}

public void setEmpId(int empId) {

    this.empId = empId;
}

public EmployeeBean(int empId, String empname) {
    this.empId = empId;
    this.empname = empname;
}

public String getEmpname() {
    return empname;
}

public void setEmpname(String empname) {
    this.empname = empname;
}

public boolean exceptionExist(){
return true;
}

}

这是我放置resultEmployeeList

的地方
public boolean getEmployeesIn(ArrayList<Integer> empids,HttpServletRequest request) {
    ArrayList<EmployeeBean> employeeList=new ArrayList<EmployeeBean>();
    Iterator empidIterator=empids.iterator();
    while(empidIterator.hasNext()){

        employeeList.add(eObject.getEmployee((Integer)empidIterator.next()));
    }
    if(employeeList.isEmpty())
        return false;
    else{
    request.setAttribute("resultEmployeeList", employeeList);
    }
    return true;

其中eObject.getEmployee(..)调用此方法..

 public EmployeeBean getEmployee(int empId) {

    EmployeeBean eb = new EmployeeBean();
    try {

        String query = "select * from empschema.employee where empid=?";
        ps = con.prepareStatement(query);
        ps.setInt(1, empId);
        ResultSet rs = ps.executeQuery();

        if (!rs.next()) {
            eb=null;
            return eb;

        } else {

                eb.setEmpId(rs.getInt(1));
                eb.setEmpname(rs.getString(2));

        }
    } catch (SQLException ex) {
        Logger.getLogger(EmployeeDAO.class.getName()).log(Level.SEVERE, null, ex);
    }
    return eb;

}

1 个答案:

答案 0 :(得分:1)

如果您按原样粘贴了代码,则在items="${resultEmployeeList} "之后${resultEmployeeList}中会有一个额外的空格。这可能导致变量element被视为String而不是实际类型。