c:forEach 抛出 javax.el.PropertyNotFoundException: Property 'foo'在 java.lang.String 类型上找不到

发布于 2024-11-10 10:37:02 字数 787 浏览 9 评论 0原文

我的项目使用 hibernate 3.4.0 GA 访问数据库,使用 Spring MVC 2.5.6 处理 Web 请求,使用 jsp(jstl) 渲染视图(网页)。

我通过 hibernate 从数据库获取实体列表,并将其作为模型添加到 jsp 的 modelmap 中。当 jsp 渲染我的网页时,它会抛出“javax.el.PropertyNotFoundException”。

javax.el.PropertyNotFoundException:在 java.lang.String 类型上找不到属性“timestamp”

,异常来自:

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

似乎“statusHistory”被视为字符串,而不是对象。

“StatusHistory”类有“timestamp”属性和getter方法:

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

我在google上搜索了一整天。有些帖子说 getter 方法不遵循约定。但看来这不是我的情况。
有人可以帮我吗?

提前致谢 安德鲁

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

枯寂 2024-11-17 10:37:02

在这里,

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

您为 items 属性提供一个值为 "statusHistoryList" 的普通字符串,其中反过来确实没有 getTimestamp() 方法。

您需要使用 EL 表达式 ${...} 来引用它。

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

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>
她如夕阳 2024-11-17 10:37:02

就我而言,我没有将 implements java.io.Serializable 添加到 bean,因此它无法识别 bean 的属性

以下是区分 JavaBean 的独特特征
来自其他 Java 类 -

它提供了一个默认的、无参数的构造函数。

它应该是可序列化的并且可以实现 Serialized 接口。

它可能具有许多可以读取或写入的属性。

它可能有许多属性的“getter”和“setter”方法。

参考tutorialspoint-JSP

添加后,它解决了问题。

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.

玩世 2024-11-17 10:37:02

保持它就像

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

确保检查 forEach 循环中的项目有时由于额外的空格它的行为就像一个字符串。尝试重新输入。

Keep it like

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

Make sure to check items in forEach loop sometimes it acts like a string due to an extra space. Try retyping it.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文