【问题标题】:JSTL not finding a property in a JavaBeanJSTL 在 JavaBean 中找不到属性
【发布时间】:2013-04-15 14:29:56
【问题描述】:

我有一个与堆栈溢出问题JSP not finding property in bean 非常相似的问题。还有这个问题javax.el.PropertyNotFoundException: Property 'foo' not found on type com.example.Bean。但是,就我而言,我认为我已经按照书本完成了所有工作,但仍然出现错误。 以下是我的 javabean 代码 sn-p 的一部分

    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>

问题是,当我评论 &lt;c:out value="${results.otheramount}"&gt;&lt;/c:out&gt; 部分时,它运行正常并且没有抛出任何错误。但是,取消注释这部分会导致找不到属性的错误。 作为旁注,属性 otheramount 是在很久以后添加的。

我使用的是 Netbeans 7.1.2。非常感谢任何帮助。

【问题讨论】:

    标签: java jsp exception jstl


    【解决方案1】:

    Bean 属性名称不是基于私有字段名称解析的。相反,它们是根据 getter 方法名称解析的。

    在您的特定情况下,属性名称因此不是otheramount,而是otherAmount

    另见:

    【讨论】:

    • 您说的很对,但我有一个问题要问。我的 getter 方法名称是 getOtherAmount。为什么我必须使用小写的'o'而不是大写的'O'?如“otherAmount”而不是“OtherAmount”。还有一个问题,我的另外两个吸气剂似乎工作正常。请原谅我的问题(我正在自学 javaEE 和 Java)
    • 我还尝试将属性名称从 otheramount 重命名为“oamt”,并将我的 getter 和 setter 分别重命名为“getOamt”和“setOamt”,当我使用 。非常感谢。
    • 这正是 JavaBeans 规范状态(另见链接)。这与Java中变量名必须以小写开头的原因完全相同。
    猜你喜欢
    • 2013-03-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-12-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-04-27
    相关资源
    最近更新 更多