bboulahdid
bboulahdid

Reputation: 339

JSP EL not working - javax.el.PropertyNotFoundException: Property 'name' not found on type java.lang.String

I have a problem with JSP EL that i figure it out. In my Servlet doGet() method i'm using this code to retrieve data from data base :

    UserServices us = new UserServices();
    List<User> users= us.allUsers();
    request.setAttribute("users", users);
    request.getRequestDispatcher("/list_users.jsp").forward(request, response);

The Problem is when i use JSP Scriplets & Expressions like this :

<%
List<User> users = (List<User>) request.getAttribute("users");
if(users != null) {
    for(User user : users) {
%>
        <p><%=user.getName()%></p>
<%
    }
}
%>

It works fine but when i use JSP EL :

<p>${user.name}</p>

this error prompt out :

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

EDIT : My user class :

class User {

    private long id;
    private String name;
    private String age;

    public User() {

    }

    public User(String name, String age) {
        this.name = name;
        this.age = age;
    }

    public long getId() {
        return id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getAge() {
        return age;
    }

    public void setAge(String age) {
        this.age = age;
    } 

}

Upvotes: 0

Views: 945

Answers (1)

vivekpansara
vivekpansara

Reputation: 899

I have tried similar code. Just add public keyword in your User class like below:

public class User {

    private long id;
    private String name;
    private String age;

   //Geters & Setters
}

Add isELIgnored="false" in <%@ page %> directive.

Upvotes: 0

Related Questions