Results 1 to 14 of 14

Thread: Problem with EL

  1. #1
    win2333 is offline Member
    Join Date
    Aug 2009
    Posts
    6
    Rep Power
    0

    Default Problem with EL

    Hi,

    I am new to J2EE.

    Code snippet for my response.jsp:

    <c:forEach var="person" items="${person}">
    <tr>
    <td><c : out value="${person.Name.firstname}"/></td>
    <td><c : out value="${person.Name.lastname}"/></td>
    </tr>
    </c:forEach>

    Why is it that i'm getting this error:

    javax.el.PropertyNotFoundException: The class 'org.apache.taglibs.standard.examples.beans.Name' does not have the property 'Name'

    Here is the snippet of Init.java

    //Persons
    Person.create("Smith", "John");
    Person.create("Shakespear", "William");
    Person.create("Galilei", "Galileo");
    Person.create("Test", "Sample");
    Person.create("Sample", "Test");
    sce.getServletContext().setAttribute("person", Person.displayAll());

    Here is the code snippet for Person.java:

    public class Person {

    private static Vector persons = new Vector();

    public Person(){}

    public static void create(String last_name, String first_name) {
    persons.add(genName(last_name,first_name));
    }

    public static Collection displayAll(){
    return persons;
    }

    public static Name genName(String last_name, String first_name) {
    return new Name(last_name, first_name);
    }
    }


    Code Snippet for Name.java

    public class Name {
    private String lastname;
    private String firstname;

    public Name() {};

    public Name(String last_name, String first_name) {
    init(last_name,first_name);
    };

    public void init(String last_name, String first_name){
    setLastName(last_name);
    setFirstName(first_name);
    }

    public void setLastName(String last_name){
    this.lastname = last_name;
    }

    public void setFirstName(String first_name){
    this.firstname = first_name;
    }

    public String getLastName(){
    return lastname;
    }

    public String getFirstName(){
    return firstname;
    }
    }

    I hope you help me on this.

    Regards,
    Nuj

  2. #2
    r035198x is offline Senior Member
    Join Date
    Aug 2009
    Posts
    2,388
    Rep Power
    7

    Default

    You are not following the Java beans naming standards.
    If you have a property called lastname, then the getter must be getLastname().
    If you have a property called lastName then the getter must be getLastName().

    The case is important.

  3. #3
    win2333 is offline Member
    Join Date
    Aug 2009
    Posts
    6
    Rep Power
    0

    Default

    Hi,

    Actually, I follow the Java beans naming standard. Please refer to the code.

    Regards,
    Nuj

  4. #4
    r035198x is offline Senior Member
    Join Date
    Aug 2009
    Posts
    2,388
    Rep Power
    7

    Default

    In your Name class you have a field called lastname and a getter called getLastName().
    Now read my post above again to see why that it wrong.

  5. #5
    win2333 is offline Member
    Join Date
    Aug 2009
    Posts
    6
    Rep Power
    0

    Default

    I already modified my code based on your suggestion. I'm still getting the error.
    The response.jsp can't access my class.
    I really appreciate your help regarding this topic because I'm still a newbie when it comes to J2EE.
    Last edited by win2333; 08-28-2009 at 09:13 AM.

  6. #6
    r035198x is offline Senior Member
    Join Date
    Aug 2009
    Posts
    2,388
    Rep Power
    7

    Default

    Somewhere in a part of your JSP that you have not posted, you have something like

    Java Code:
    "${person.Name.Name}"

  7. #7
    win2333 is offline Member
    Join Date
    Aug 2009
    Posts
    6
    Rep Power
    0

    Default

    No, i don't have this kind of code "${person.Name.Name}"

  8. #8
    r035198x is offline Senior Member
    Join Date
    Aug 2009
    Posts
    2,388
    Rep Power
    7

    Default

    Ok so you edited your post above to something else.
    What do you mean by "The response.jsp can't access my class."?
    Are you getting an error message? If so then post the error that you are getting now.

  9. #9
    win2333 is offline Member
    Join Date
    Aug 2009
    Posts
    6
    Rep Power
    0

    Default

    Here is the content of RESPONSE.JSP

    <html>
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <title>JSP Page</title>
    </head>
    <body>

    <h1>response.jsp Page</h1>

    <jsp:useBean id="mybean" scope="session" class="org.me.hello.NameHandler" />
    <jsp:setProperty name="mybean" property="*" />
    Hello, <jsp:getProperty name="mybean" property="name" />
    <br>
    <br>

    <%-- Exercise Expression Language --%>
    <table border="1">
    <%--
    <c:forEach var="customer" items="${customers}">
    <tr>
    <td><c:out value="${customer.lastName}"/></td>
    <td><c:out value="${customer.address.city}"/></td>
    <td><c:out value="${customer.address.state}"/></td>
    <td><c:out value="${customer.address.zip}"/></td>
    <td><c:out value="${customer.phoneHome}" default="no home phone specified"/></td>
    </tr>
    </c:forEach>
    --%>

    <c:forEach var="personlist" items="${person}">
    <tr>
    <td><c:out value="${personlist.First_Name}"/></td>
    <td><c:out value="${personlist.LastName}"/></td>
    </tr>
    </c:forEach>
    </table>

    </body>
    </html>

  10. #10
    r035198x is offline Senior Member
    Join Date
    Aug 2009
    Posts
    2,388
    Rep Power
    7

    Default

    What error messages do you get?

    What type of objects are in the lists ${customers} and ${person}?

  11. #11
    win2333 is offline Member
    Join Date
    Aug 2009
    Posts
    6
    Rep Power
    0

    Default

    javax.el.PropertyNotFoundException: The class 'org.apache.taglibs.standard.examples.beans.Name' does not have the property 'firstname'

    it's a collection. please refer above under the code snippet for Person.java, Init.java and Name.java

  12. #12
    r035198x is offline Senior Member
    Join Date
    Aug 2009
    Posts
    2,388
    Rep Power
    7

    Default

    So you did not change your code after all?
    Read that message and all the posts I've been making up to.
    class Name's firstname property must have the getter getFirstname() with a lower case n.
    If you want the getter to be getFirstName with an upper case N then the property must be called firstName with an upper case N.

  13. #13
    chandra1425 is offline Member
    Join Date
    Sep 2010
    Posts
    1
    Rep Power
    0

    Default

    hi,
    this is chandra, if u set attribute in session, u write foreach items like ${sessionScope.person}

  14. #14
    Hossein is offline Member
    Join Date
    Apr 2010
    Posts
    10
    Rep Power
    0

    Default EL rules!

    If you still haven't solved the problem...

    Change this:
    Java Code:
    <tr>
    <td><c:out value="${personlist.First_Name}"/></td>
    <td><c:out value="${personlist.LastName}"/></td>
    </tr>
    to this:

    Java Code:
    <tr>
    <td><c:out value="${personlist.firstName}"/></td>
    <td><c:out value="${personlist.lastName}"/></td>
    </tr>
    I think it should work.

    because:

    when you type:
    Java Code:
    ${foo.bar}
    this happens:
    Since foo is not an implicit EL object, the container starts from page scope up to application scope and looks for the first "attribute" it finds with name "foo". You don't need to specify a scope. It is good to specify though.
    Like:
    Java Code:
    ${pageScope.personlist.firstName}
    to avoid naming conflicts with other scopes.
    (here it doesn't matter at all since you have set the personlist attribute in your pageScope. by saying: var="personlist". The container will find that at the first try in the page scope)
    Again in your forEach tag you would use:
    Java Code:
    items="${applicationScope.person}"
    Since you used ServletContext to set the person attribute and that means applicationScope.

    Then after finding the foo attribute anyway, in the corresponding object it calls ... wait for it... getBar() method
    Pay attention to the uppercase B here.
    you don't need to have a "bar" property in your class. you just need to have a getBar() method. When you use a ${foo.bar} the container looks for that method. It doesn't care how the method works. whether if it returns a bar property or it just calculates it out of thin air! It does not matter
    the general rule is: In EL when you wanna say get something from a java bean, you have to have a getter method. then you drop the "get" from the method name and you change the first letter to lowercase. easy peasy!

    so I think ${personlist.firstName} should work because you have getFirstName() method in your class. Same thing for the guy's last name.;)

    After all this... Some one correct me if I'm wrong please. :D

    P.S. You might wanna change some names up there in your code. Just some better names to be more... becoming.
    Last edited by Hossein; 10-02-2010 at 08:27 PM.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •