欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

JavaBean命名规范,不能以一个字母开头bPrivNum,可以bussPrivNum

程序员文章站 2022-07-05 09:10:11
...

今天做项目时候遇到个JavaBean的set()、get()方法取值取不到问题!!!!

 

###1.今后注意JavaBean或Model的命名尽量不要使用如下形式:

即:1)首字母别一个小写开头,2)结尾尽量不要加s复数

  private String privNum;//正确
  private String bPrivNum;//错误

  private Double[] selectMoney;//正确
  private Double[] selectMoneys;//错误

 

 

报如下错误:

type Exception report

message

description The server encountered an internal error () that prevented it from fulfilling this request.

exception
org.apache.jasper.JasperException: An exception occurred processing JSP page 
/jsp/phone/login/frame/frameLeft.jsp at line 97

94:
95:
96: <c:forEach items="${firstList1}" var="firstMenu">
97: ${firstMenu.bPrivName}
98: </c:forEach>
99: <c:forEach items="${secondList1}" var="secondMenu">
100: ${secondMenu.bPrivName}

 root cause
javax.el.PropertyNotFoundException: Property 'bPrivName' not found on type 
com.hanpeng.base.phone.model.TBussinessPriv
    javax.el.BeanELResolver$BeanProperties.get(BeanELResolver.java:193)
    javax.el.BeanELResolver$BeanProperties.access$400(BeanELResolver.java:170)
    javax.el.BeanELResolver.property(BeanELResolver.java:279)
    javax.el.BeanELResolver.getValue(BeanELResolver.java:60)
    javax.el.CompositeELResolver.getValue(CompositeELResolver.java:53)

 ###大概意思为:el表达式没法通过getXXX()找打TbussinessPriv中的bPrivName属性

 

一、总结

总结如下:EL表达式调用BeanUtils.getProperty(bean, name);来获得对象属性,分析源码这个类默认将你javaBean

的属性,首字母大写并且前面加上set、get,例如privNum-->setPrivNum、getPrivNum。

但是如果你属性为bPrivNum-->setBPrivNum、getBPrivNum,你通过EL表达式${test.bPrivNum}就会报错!!!


二、如何避免

1.JavaBean命名时避免出现bPrivNum这样的名字,1)可以为privNum。2)可以为BPrivNum。

public class TBussinessPriv extends BaseModel{

	private String privNum;
	//private String BPrivNum; 也正确
	//private String bPrivNum; 错误

	public String getPrivNum() {
	
		return privNum;
	}

	public void setPrivNum(String privNum) {
	
		this.privNum = privNum;
	} 
}
 

2.非要使用bPrivNum命名,EL表达式得值时,人为改为BPrivNum,不能更JavaBean中的bPrivNum一样

public class TBussinessPriv extends BaseModel{

	private String bPrivNum;

	public String getbPrivNum() {
	
		return bPrivNum;
	}

	public void setbPrivNum(String bPrivNum) {
	
		this.bPrivNum = bPrivNum;
	}
}
 
<c:if test="${firstList!=null}">
<c:forEach items="${firstList}" var="firstMenu">
<h1 id="m${firstMenu.BPrivNum}" 
  onClick="SwitchMenu('sub${firstMenu.BPrivNum}','m${firstMenu.BPrivNum}')" class="menu">
<ol class="left11">
${firstMenu.BPrivName}
</ol>
</h1>
<ul class="submenu" id="sub${firstMenu.BPrivNum}" >
<c:forEach items="${secondList}" var="secondMenu">
<c:if test="${firstMenu.BPrivNum==secondMenu.BMenuId1}">
<li><a href="${secondMenu.BPrivUrl}" target="main">
<span><img src="${secondMenu.BPicUrl}" width="26" height="27" align="middle"/></span>
${secondMenu.BPrivName}</a></li><br/>
</c:if>
</c:forEach>
</ul>
</c:forEach>
</c:if>