2

I have a problem that is very similar to this stack overflow question JSP not finding property in bean. And also this question javax.el.PropertyNotFoundException: Property 'foo' not found on type com.example.Bean. However in my case I think that i have done everything by the books and I am still getting an error. Below is part of my javabean code snippet

    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;        
            }

Below is part of my DAO code

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);



         }

Below is part of the servlet code

try
{
ArrayList allresults = mydao.search();    
request.setAttribute("allresults",allresults);
RequestDispatcher dispatch =request.getRequestDispatcher("Pages/mypage.jsp");
dispatch.forward(request, response);
}
catch(Exception ex)
{
}

Below is my HTML and JSTL code in the JSP page

<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>

The problem is that when I comment the part <c:out value="${results.otheramount}"></c:out> it runs okay and no errors are thrown. However uncommenting this part results in the error of property not found. As a side note, the property otheramount was added much later.

I am using Netbeans 7.1.2. Any help greatly appreciated.

Community
  • 1
  • 1
MaxI
  • 763
  • 2
  • 10
  • 43

1 Answers1

6

Bean property names are not resolved on basis of private field names. Instead, they are resolved on basis of getter method names.

In your particular case, the property name is thus not otheramount, but it is instead otherAmount.

See also:

Community
  • 1
  • 1
BalusC
  • 992,635
  • 352
  • 3,478
  • 3,452
  • You are very right but I have a question to ask. My getter method name is getOtherAmount. How come I have to use lowercase 'o' instead of uppercase 'O'? As in 'otherAmount' instead of 'OtherAmount'. And another question, my other two getters seem to work okay. Kindly excuse my questions(I'm teaching myself javaEE and Java in general) – MaxI Apr 15 '13 at 14:44
  • I also tried renaming the property name from otheramount to 'oamt' and renamed my getter and setter as 'getOamt' and 'setOamt' respectively and it worked when I used . Thanks a lot. – MaxI Apr 15 '13 at 14:59
  • That's just what JavaBeans specification state (see also the link). It's exactly the same reason why variable names in Java must start with a lowercase. – BalusC Apr 15 '13 at 15:22