PDA

View Full Version : jsp c tag


maveganzones
Feb 7th, 2006, 04:40 AM
Hi all.

I have a class named "Class1" with two attributes "id" and "name" and their respectives setters & getters.

There is a second class "Class2" extending "Class1" with an attribute "age" plus the getter and the setter.

In the web flow i return a jsp page as view with a list of elements of type "Class2" as model.

In the jsp page i do the following:

<c:forEach items="${model.class2}" var="item">
<td>
<p>Name: <c: out value="${item.name}"/></p>
<p>Age: <c: out value="${item.age}"/></p>
</td>
</c:forEach>

Tomcat launchs an error for item.name:
Unable to find a value for "name" in object of class "Class2" using operator "." (null)

However, if a let only the second value it works:
<c:forEach items="${model.class2}" var="item">
<td>
<p>Age: <c: out value="${item.age}"/></p>
</td>
</c:forEach>

What can be the problem? Any suggestions?

Colin Yates
Feb 7th, 2006, 05:09 AM
What is the visibility of name? Do you have a getName() (not sure if it needs a setName(); don't think so)?

maveganzones
Feb 7th, 2006, 06:00 AM
All the attributes are private and have a getter and a setter, for example:

private String name;

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

public String getName() {
return this.name;
}

Colin Yates
Feb 7th, 2006, 06:05 AM
OK, can you override toString() in ClassA and ClassB with the following:


public final String toString() {
return getClass().getName() + "; name: [" + getName() + "]; age: [" + getAge() + "]";
}


then in your forEach loop do:

${item}


and post the results please :)

maveganzones
Feb 7th, 2006, 06:33 AM
Solved :)

the getName() method was specified as getNAme() so the bean definition was corrupted. I dont know how cant i see it.

Thx a lot