布尔字段的Java Bean规范 [英] Java Bean Specifications for boolean field
问题描述
我的Java bean的字段为activeRecord
I have Java bean with the field activeRecord
private Boolean activeRecord;
@Override
public Boolean isActiveRecord() {
return activeRecord;
}
@Override
public void setActiveRecord(Boolean activeRecord) {
this.activeRecord = activeRecord;
}
当我在列表中将其作为Jasper Report数据源发送时
when I send it in List as Jasper Report data source
List<Branch> dataList = new BranchLogic().selectAll();
JRBeanCollectionDataSource beanColDataSource = new JRBeanCollectionDataSource(dataList);
我收到错误消息
net.sf.jasperreports.engine.JRException:从bean:activeRecord检索字段值时出错. .... 由以下原因引起:java.lang.NoSuchMethodException:属性'activeRecord'在类'com.tawaak.app.data.domain.model.branch.Branch'类中没有getter方法.
net.sf.jasperreports.engine.JRException: Error retrieving field value from bean: activeRecord. .... Caused by: java.lang.NoSuchMethodException: Property 'activeRecord' has no getter method in class 'class com.tawaak.app.data.domain.model.branch.Branch'
为什么Jasper无法将isActiveRecord
识别为一种吸气剂方法?
Why Jasper doesn't recognize the isActiveRecord
as a getter method?
推荐答案
前缀is...
可用于返回原始boolean
的方法.但是,字段activeRecord
的类型为Boolean
,它是一个对象(包装类型为boolean
),对于对象,您始终需要使用get...
.
The prefix is...
can be used for methods that return a primitive boolean
. However, your field activeRecord
is of type Boolean
, which is an object (the wrapper type of boolean
), and for objects you always need to use get...
.
来自 JavaBeans规范,8.3.2:
此外,对于
boolean
属性,我们允许使用getter方法来匹配模式:
In addition, for
boolean
properties, we allow a getter method to match the pattern:
public boolean is<PropertyName>();
此is<PropertyName>
方法可以代替get<PropertyName>
方法来提供,或者可以是get<PropertyName>
方法的补充.
This is<PropertyName>
method may be provided instead of a get<PropertyName>
method, or it may be provided in addition to a get<PropertyName>
method.
这样,您有两个可能的解决方法:
As such, you have two possible fix:
- 使您的
activeRecord
成为boolean
,并保持吸气剂isActiveRecord()
.如果activeRecord
不能为null
,则这将是首选方法. - 将其保留为
Boolean
,但将方法isActiveRecord()
重命名为getActiveRecord()
.您需要确保呼叫者能够正确处理null
.
- Make your
activeRecord
aboolean
and keep the getterisActiveRecord()
. This would be the preferred approach ifactiveRecord
cannot benull
. - Keep it as a
Boolean
, but rename your methodisActiveRecord()
togetActiveRecord()
. You'll need to make sure the caller handlesnull
properly.
这篇关于布尔字段的Java Bean规范的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!