访问传递给jasper记录的唯一Java bean [英] Accessing the only Java bean passed to jasper records

查看:131
本文介绍了访问传递给jasper记录的唯一Java bean的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要使用 JasperReports 创建PDF文件。问题是它只需要一个Java bean作为参数(不是列表也不是数组),并且报表具有无法在表或列表中表达的自定义布局。如果我执行以下代码,当jrxml只有布局而没有打印参数时 - PDF显示OK。这是我尝试传递参数的方式:

I need to create a PDF file using JasperReports. The problem is that it has to take only one Java bean as a parameter (not list nor array) and the report has custom layout that can not be expressed in tables or lists or whatever. If I execute the following code, when jrxml has only layout and no printing of the parameters - PDF displays OK. This is how I try to pass a parameter:

Map<String, Object> map = new HashMap<String, Object>();
url = getClass().getClassLoader().getResource("/WEB-INF/reports/reg.jrxml");
jasperReport = JasperCompileManager.compileReport(url.getPath());
map.put("PARAM_NAME", myBean);
JasperPrint jasperPrint = JasperFillManager.fillReport(jasperReport, 
    map, new JREmptyDataSource());
JasperExportManager.exportReportToPdfStream(jasperPrint, responseOutputStream);

问题是,如何配置我的jrxml文件,以便我可以访问<$的字段c $ c> myBean ,以便我可以填写文本字段?我尝试添加

The question is, how should I configure my jrxml file so that I have access to fields of myBean, so that I can fill in text fields? I have tried adding

<parameter name="PARAM_NAME" class="net.sf.jasperreports.engine.JRDataSource"/>

到jrxml文件,但我仍然无法访问它的字段。比如说, myBean 有一个字段 uid getUid()相应的,我需要在某处打印。

to jrxml file but I still can't access it's fields. Say, myBean has a field uid and getUid() correspondingly and I need to print it somewhere.

推荐答案

不知道为什么我从一开始就不能这样做。 ..将Java代码的部分更改为:

Don't know why I couldn't do this from the very beginning... Changed parts of Java code to:

List<IBClientBean> data = new ArrayList<MyBean>();
data.add(myBean);
JasperPrint jasperPrint = JasperFillManager.fillReport(jasperReport, 
    new HashMap<String, Object>(), new JRBeanCollectionDataSource(data));

并在声明属性后立即手动添加到jrxml文件的开头(而不是我尝试过的)添加问题):

and added manually to the beginning of jrxml file, right after declaration of properties (instead of what I tried to add in the question):

<field name="uid" class="java.lang.String"/>

等等。然后我就可以打印 uid 到pdf,只需在文本字段中输入 $ F {uid}

and so on for all required fields in MyBean. Then I was able to print uid to pdf simply typing $F{uid} inside of Text Field.

这篇关于访问传递给jasper记录的唯一Java bean的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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