I am creating a simple guest book in JSP in order to learn this technology. Currently I have two classes: guestbook/GuestBook.class and guestbook/Entry.class (I haven’t finished the app yet, so I have only these classes) which are added into WEB-INF/libs/ and they’re included properly. In my file index.jsp I am using guestbook.GuestBook class; its method returns Vector. When I iterate over entries and I’d like to print an author of the entry, I can see:
javax.el.PropertyNotFoundException: Property 'author' not found on type guestbook.Entry
I must add that Entry class is public and author attribute is declared in such way:
public String author;
So it is public, too. This is my code when I iterate over the entries:
<c:forEach items="${entries}" varStatus="i">
<c:set var="entry" value="${entries[i.index]}" />
<li><c:out value="${entry.author}" /></li>
</c:forEach>
and
entry.class.name
returns guestbook.Entry
The classes are in package guestbook (as you can guess), entries vector is passed to pageContext.
I do not know what is wrong with my way of doing it. Can anybody help me please with that? (Thanks in advance!)
JSP EL will not recognise public fields in your classes, it only works with getter methods (which is good practice anyway – never expose your classes’ state as public fields like this).
So use
instead of
As a side note, your JSTL is overly complicated, it can be simplified to:
or even
although the latter form will not XML-escape the author name, so isn’t advised.
Lastly, the
Vectorclass is obsolete, you should useArrayListinstead.