我有一个与此堆栈溢出问题非常相似的问题 JSP not finding property in bean 。还有这个问题javax.el.PropertyNotFoundException: Property 'foo' not found on type com.example.Bean 。然而,就我而言,我认为我已经按照书本做了所有事情,但仍然遇到错误。 下面是我的 javabean 代码片段的一部分
private double otheramount;
private int no;
private String name;
public double getOtherAmount()
{
return otheramount;
}
public void setOtherAmount(double newotheramount)
{
otheramount = newotheramount;
}
public int getNo()
{
return no;
}
public void setNo(int newno)
{
no = newno;
}
public String getName()
{
return name;
}
public void setName(String newname)
{
name = newname;
}
下面是我的 DAO 代码的一部分
while(rs.next())
{
MyBean mybean = new MyBean();
mybean.setNo(rs.getInt("No"));
mybean.setName(rs.getString("Full_Names"));
mybean.setOtherAmount(rs.getDouble("OtherAmount"));
allresults.add(mybean);
}
下面是 servlet 代码的一部分
try
{
ArrayList allresults = mydao.search();
request.setAttribute("allresults",allresults);
RequestDispatcher dispatch =request.getRequestDispatcher("Pages/mypage.jsp");
dispatch.forward(request, response);
}
catch(Exception ex)
{
}
下面是我在 JSP 页面中的 HTML 和 JSTL 代码
<c:forEach var="results" items="${requestScope.allresults}">
<tr>
<td><c:out value="${results.no}"></c:out></td>
<td><c:out value="${results.name}"></c:out></td>
<td><c:out value="${results.otheramount}"></c:out></td>
</tr>
</c:forEach>
问题是当我评论 <c:out value="${results.otheramount}"></c:out>
部分时它运行正常并且没有抛出任何错误。然而,取消注释这部分会导致找不到属性的错误。
附带说明一下,属性 otheramount 是很晚才添加的。
我使用的是 Netbeans 7.1.2。非常感谢任何帮助。
最佳答案
Bean 属性名称不是根据私有(private)字段名称解析的。相反,它们是根据 getter 方法名称来解析的。
在您的特定情况下,属性名称因此不是 otheramount
,而是 otherAmount
。
另请参阅:
关于java - JSTL 未在 JavaBean 中找到属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16017777/