-
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
-
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.
-
Hi,
Actually, I follow the Java beans naming standard. Please refer to the code.
Regards,
Nuj
-
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.
-
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.
-
Somewhere in a part of your JSP that you have not posted, you have something like
Code:
"${person.Name.Name}"
-
No, i don't have this kind of code "${person.Name.Name}"
-
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.
-
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>
-
What error messages do you get?
What type of objects are in the lists ${customers} and ${person}?
-
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
-
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.
-
hi,
this is chandra, if u set attribute in session, u write foreach items like ${sessionScope.person}
-
EL rules!
If you still haven't solved the problem...
Change this:
Code:
<tr>
<td><c:out value="${personlist.First_Name}"/></td>
<td><c:out value="${personlist.LastName}"/></td>
</tr>
to this:
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:
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:
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:
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.