在eclipse中自动生成setters()和getters()
User.java
private boolean active ;
//getter()
public boolean isActive() {
return isActive;
}
//setter()
public void setActive(boolean active ) {
this.active = active;
}
//jsp 页面中
${user.isActive}
==error==
javax.servlet.jsp.el.ELException: Unable to find a value for “isActive” in object of class “com.mycompany.domain.User” using operator “.”
==cuz==
JavaBean命名规范里面规定,对于primitive和自定义类类型的属性property(小写),
getter和setter方法就是getProperty和setProperty(第一个字母变大写,前面再加get或set)。
而对于类型为boolean的属性,getter方法还可以写为isProperty(getProperty仍然可用),
而且,如果属性的前两个字母是is(两个小写),则getter与property完全同名(比如上面属性isActive的getter就是isActive())。
① JSP里只能用${user.active}来访问(注意大小写)。
② 把active的getter()手动写为getActive(),JSP还是和其他非boolean型属性一样使用。
-------------------------------------------------------------------------------------------------------------------------------------------------
1. UserAdress 这样的字段() 为避免出错 声明的时候 private String userAdress; 这样生成的setter&getter就比较规范,符合javaBean的规范
方便使用。
public void setUserAdress(String userAdress) {
this.userAdress = userAdress;
}
public String getUserAdress() {
return userAdress;
}
2. PDFSize 这样的字段可以不用做任何修改。(下面引用中会讲到URL)
3. cInfo(一般性地说,第一个字母小写,第二个字母大写属性命名在开发需求中经常有遇到,比如c是代表用户数据,i代表输入的数据) 这样的字段需要把I变回i,总而言
之: 命名属性时,第二个字符最好是小写字母。这个方法不需要做更多地修改,符合所有规范,最为稳妥。
否则自动生成的setter&getter都是setCInfo(),getCInfo();运行出错:
Caused by: org.hibernate.PropertyNotFoundException: Could not find a getter for cInfo in class User
at org.hibernate.property.BasicPropertyAccessor.createGetter(BasicPropertyAccessor.java:282)
at org.hibernate.property.BasicPropertyAccessor.getGetter(BasicPropertyAccessor.java:275)
---------------------------------------------------------------------------------------------------------------------------------------------------
引用 http://www.360doc.com/content/08/1231/15/94066_2235043.shtml
跟踪到org.hibernate.property.BasicPropertyAccessor类中的getterMethod(Class theClass, String propertyName)方法:1
private static Method getterMethod(Class theClass, String propertyName) {
Method[] methods = theClass.getDeclaredMethods();
for (int i = 0; i < methods.length; i++) {
// only carry on if the method has no parameters
if (methods[i].getParameterTypes().length == 0) {
String methodName = methods[i].getName();
// try "get"
if (methodName.startsWith("get")) {
String testStdMethod = Introspector.decapitalize(methodName.substring(3));
String testOldMethod = methodName.substring(3);
if (testStdMethod.equals(propertyName) || testOldMethod.equals(propertyName)) {
return methods[i];
}
}
// if not "get" then try "is"
if (methodName.startsWith("is")) {
String testStdMethod = Introspector.decapitalize(methodName.substring(2));
String testOldMethod = methodName.substring(2);
if (testStdMethod.equals(propertyName) || testOldMethod.equals(propertyName)) {
return methods[i];
}
}
}
}
return null;
}
getterMethod()对Company类中声明的方法进行遍历,找到与属性名匹配的方法,即属性的getter方法。比较分两部分,第一部分,针对primitive和自定义类类型的属性;第二部分,针对boolean类型的属性(由于boolean类型属性的getter方法的特殊性)。
跟踪发现,methodName的值为“getSAddress”,propertyName的值为“sAddress”,testOldMethod的值为“SAddress”,testStdMethod的值为“SAddress”。testStdMethod和testOldMethod相同,而它们都不匹配propertyName!
因此,getterMethod()中找不到与属性sAddress匹配的getter方法,getterMethod()返回null,导致异常。
问题出在Introspector.decapitalize()方法。
decapitalize()源码如下:1
/**
* Utility method to take a string and convert it to normal Java variable
* name capitalization. This normally means converting the first
* character from upper case to lower case, but in the (unusual) special
* case when there is more than one character and both the first and
* second characters are upper case, we leave it alone.
*
* Thus "FooBah" becomes "fooBah" and "X" becomes "x", but "URL" stays
* as "URL".
*
* @param name The string to be decapitalized.
* @return The decapitalized version of the string.
*/
public static String decapitalize(String name) {
if (name == null || name.length() == 0) {
return name;
}
if (name.length() > 1 && Character.isUpperCase(name.charAt(1)) &&
Character.isUpperCase(name.charAt(0))){
return name;
}
char chars[] = name.toCharArray();
chars[0] = Character.toLowerCase(chars[0]);
return new String(chars);
}