?
Solved

Using int data type for member of Java Bean

Posted on 2006-04-29
4
Medium Priority
?
335 Views
Last Modified: 2012-06-21
Hi Guys,
I have problems using a member of data type int in Java Bean
Below are the relevant parts of my Java Bean class file, Student.class
private int Count;

public int getCount()
{
  return Count;
}
      
public void setCount(int Count)
{
    this.Count = Count;
}

JSP file which uses Student bean:
The below jsp file is actioned when I submit a form as part of another jsp file
It is simply set up to print out the Count and FirstName properties of the Student Java bean
It does print out the FirstName that I supply in the form correctly, but no matter which number I supply in Count in the form, 0(zero) is always returned for Count.
It is as if the Java Bean is not handling property of type int!
Maybe there is something else I need to be doing using another data type other than int or qualifying this more or soemething - Any advice appreciated.

<%@page contentType="text/html"%>
<%@page import="ie.nci.student.*"%>
<jsp:useBean id="student" scope="page" class="ie.nci.student.Student" />
<jsp:setProperty name="student" property="*"/>
<html>
<head><title>Update Student</title></head>
<body>
<%
    out.println(student.getCount());
      out.println(student.getFirstName());
      
%>
 
</body>
</html>
0
Comment
Question by:Barry Cunney
  • 2
  • 2
4 Comments
 
LVL 28

Expert Comment

by:rrz
ID: 16571044
Try it this way.  The field in the form shoud be "count".  
private int count;
public int getCount()
{
  return count;
}
     
public void setCount(int count)
{
    this.count = count;
}
0
 
LVL 17

Author Comment

by:Barry Cunney
ID: 16572463
Hi rrz@871311
Thank you very much for that - this was a big help
I am just learning Java Beans

Is it a documented rule that the bean class member variable names should be lower case? or is there some other reason for this?

I also noticed that in one of my jsp files I had enctype="text/plain" as one of the attributes for my form tag, and this also casue problems with numeric values being passed into the bean.

0
 
LVL 28

Accepted Solution

by:
rrz earned 280 total points
ID: 16572528
>"text/plain"
that is a MIME type, see  for example   http://www.sympa.org/distribution/current/wwsympa/mime.types   
>Is it a documented rule that the bean class member variable names should be lower case?
You look at this.     http://java.sun.com/products/javabeans/docs/spec.html     But the relevent parts are below.
From the javabean specification,
8.8 Capitalization of inferred names.
When we use design patterns to infer a property or event name, we need to decide what rules
to follow for capitalizing the inferred name. If we extract the name from the middle of a normal
mixedCase style Java name then the name will, by default, begin with a capital letter.
Java programmers are accustomed to having normal identifiers start with lower case letters.
Vigorous reviewer input has convinced us that we should follow this same conventional rule
for property and event names.
JavaBeans Introspection
Sun Microsystems 58 10/12/97
Thus when we extract a property or event name from the middle of an existing Java name, we
normally convert the first character to lower case. However to support the occasional use of all
upper-case names, we check if the first two characters of the name are both upper case and if
so leave it alone. So for example,
“FooBah” becomes “fooBah”
“Z” becomes “z”
“URL” becomes “URL”

8.3
By default, we use design patterns to locate properties by looking for methods of the form:
public <PropertyType> get<PropertyName>();
public void set<PropertyName>(<PropertyType> a);
If we discover a matching pair of “get<PropertyName>” and “set<PropertyName>” methods
that take and return the same type, then we regard these methods as defining a read-write property
whose name will be “<propertyName>”. We will use the “get<PropertyName>” method
to get the property value and the “set<PropertyName>” method to set the property value. The
pair of methods may be located either in the same class or one may be in a base class and the
other may be in a derived class.
If we find only one of these methods, then we regard it as defining either a read-only or a writeonly
property called “<propertyName>”
By default we assume that properties are neither bound nor constrained (see Section 7).
So a simple read-write property “foo” might be represented by a pair of methods:
public Wombat getFoo();
public void setFoo(Wombat w);

Tere are a few "gottchas". Consider the following bean.
package test;
public class BeanTest implements java.io.Serializable {
   private boolean[] bHours = {false,true};
   private String RRZ = "me";
   private String Joe = "hello";
   public BeanTest(){
   }
   public void setbHours(boolean[] bHours){
          this.bHours = bHours;
   }
   public boolean[] getbHours(){
          return bHours;
   }
   public void setRRZ(String str){
                            RRZ = str;
   }
   public String getRRZ(){
          return RRZ;
   }
   public void setJoe(String str){
                            Joe = str;
   }
   public String getJoe(){
          return Joe;
   }
}
In a JSP the following display the correct result.
${myBean.RRZ}
${myBean.joe}
${myBean.bHours[0]}    

I hope I am not confusing you. Just stick to the standard and you won't have a problem.   rrz
0
 
LVL 17

Author Comment

by:Barry Cunney
ID: 16572545
That's brilliant help rrz

Thanks again
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Often times it's very very easy to extend a volume on a Linux instance in AWS, but impossible to shrink it. I wanted to contribute to the experts-exchange community a way of providing a procedure that works on an AWS instance. It can also be used on…
MSSQL DB-maintenance also needs implementation of multiple activities. However, unprecedented errors can hamper the database management. In that case, deploying Stellar SQL Database Toolkit ensures fast and accurate database and backup repair as wel…
Integration Management Part 2
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an anti-spam), the admin…
Suggested Courses
Course of the Month16 days, 15 hours left to enroll

862 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question