c:forEach throws javax.el.PropertyNotFoundException: Property 'foo' not found on type java.lang.String

不想你离开。 提交于 2019-11-26 04:01:31

问题


My project is using hibernate 3.4.0 GA to access database, and Spring MVC 2.5.6 to handle web request and jsp(jstl) to render view(web page).

I get an entity list from database, by hibernate, and add it as model into modelmap for jsp.When jsp rendering my webpage, it throws a \"javax.el.PropertyNotFoundException\".

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

and the exception comes from:

<c:forEach var=\"statusHistory\" items=\"statusHistoryList\">
    ${statusHistory.timestamp}
</c:forEach>

It seems like that \"statusHistory\" is considered as a String, but not an object.

The \"StatusHistory\" class has \"timestamp\" property and the getter method:

public Class StatusHistory{
    ...
    private Date timestamp;
    public Date getTimestamp(){...}
    ...
}

I have searched on google for one whole day. Some post says that the getter method is not following the convention. But it seems it\'s not my case.
can some one please help me?

Thanks in advance Andrew


回答1:


Here,

<c:forEach var="statusHistory" items="statusHistoryList">

You're supplying the items attribute of <c:forEach> with a plain vanilla String with a value of "statusHistoryList" which in turn indeed doesn't have a getTimestamp() method.

You need to reference it using an EL expression ${...} instead.

<c:forEach var="statusHistory" items="${statusHistoryList}">
    ${statusHistory.timestamp}
</c:forEach>



回答2:


In my case I had not added implements java.io.Serializable to the bean so it was not identifying bean's property

Following are the unique characteristics that distinguish a JavaBean from other Java classes −

It provides a default, no-argument constructor.

It should be serializable and that which can implement the Serializable interface.

It may have a number of properties which can be read or written.

It may have a number of "getter" and "setter" methods for the properties.

Reference tutorialspoint-JSP

Once added it fixed the issue.



来源:https://stackoverflow.com/questions/6146808/cforeach-throws-javax-el-propertynotfoundexception-property-foo-not-found-on

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!