简体   繁体   English

布尔字段的Java Bean规范

[英]Java Bean Specifications for boolean field

I have Java bean with the field activeRecord 我有Java bean,字段为activeRecord

private Boolean activeRecord;

@Override
public Boolean isActiveRecord() {
    return activeRecord;
}

@Override
public void setActiveRecord(Boolean activeRecord) {
    this.activeRecord = activeRecord;
}

when I send it in List as Jasper Report data source 当我在List中作为Jasper Report数据源发送它时

List<Branch> dataList = new BranchLogic().selectAll();
JRBeanCollectionDataSource beanColDataSource = new JRBeanCollectionDataSource(dataList);

I get the error message 我收到错误消息

net.sf.jasperreports.engine.JRException: Error retrieving field value from bean: activeRecord. net.sf.jasperreports.engine.JRException:从bean:activeRecord检索字段值时出错。 .... Caused by: java.lang.NoSuchMethodException: Property 'activeRecord' has no getter method in class 'class com.tawaak.app.data.domain.model.branch.Branch' ....引起:java.lang.NoSuchMethodException:属性'activeRecord'在类'class com.tawaak.app.data.domain.model.branch.Branch'中没有getter方法

Why Jasper doesn't recognize the isActiveRecord as a getter method? 为什么Jasper不将isActiveRecord识别为getter方法?

The prefix is... can be used for methods that return a primitive boolean . 前缀is...可用于返回原始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... . 但是,你的字段activeRecordBoolean类型,它是一个对象( boolean的包装类型),对于你总是需要使用get...对象。

From the JavaBeans specification , 8.3.2: JavaBeans规范 ,8.3.2:

In addition, for boolean properties, we allow a getter method to match the pattern: 另外,对于boolean属性,我们允许getter方法匹配模式:

 public boolean is<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. 这可以提供is<PropertyName>方法而不是get<PropertyName>方法,或者除了get<PropertyName>方法之外还可以提供它。

As such, you have two possible fix: 因此,您有两种可能的解决方法:

  • Make your activeRecord a boolean and keep the getter isActiveRecord() . activeRecord设为boolean并保持getter isActiveRecord() This would be the preferred approach if activeRecord cannot be null . 如果activeRecord不能为null这将是首选方法。
  • Keep it as a Boolean , but rename your method isActiveRecord() to getActiveRecord() . 将其保留为Boolean ,但将方法isActiveRecord()重命名为getActiveRecord() You'll need to make sure the caller handles null properly. 您需要确保调用者正确处理null

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM