布尔字段的Java Bean规范 [英] Java Bean Specifications for boolean field

查看:83
本文介绍了布尔字段的Java Bean规范的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的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 a boolean and keep the getter isActiveRecord(). This would be the preferred approach if activeRecord cannot be null.
  • Keep it as a Boolean, but rename your method isActiveRecord() to getActiveRecord(). You'll need to make sure the caller handles null properly.

这篇关于布尔字段的Java Bean规范的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆