ArrayList of Bean..How to access the property of a bean using EL in jsp..? Throws NotFoundError exception

StackOverflow https://stackoverflow.com/questions/23492233

Question

I have an arraylist of employeebean resultEmployeeList set in request scope.. I don't know if i was doing it the right way.. But here is the jstl code used in jsp..

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

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


            </tr>
        </c:forEach>

when i try to access empId property of employeebean, it shows this error

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

Here is the 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;
}

}

This is where i put the 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;

Wherein eObject.getEmployee(..) calls this method..

 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;

}
Was it helpful?

Solution

If you have pasted the code as is, then there is an extra space in items="${resultEmployeeList} " after ${resultEmployeeList}. That can cause the variable element to be treated as a String instead of the actual type.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top