olu_guy Posted November 21, 2006 Share Posted November 21, 2006 Hi All, this question has been asked several times on this forum and other forums but unfortunately there's been no post (s) that actually provides a proper example or solution. I am trying to use javabeans as the datasource for my jasper reports. Does anyone have an example on how to implement this step by step ? I have seen some examples but none of them addresses the problem or provide a step by step guide. If there links to tutorials on using javabeans as a datasource in ireports or jasper reports that will be fully appreciated. Cheers. Link to comment Share on other sites More sharing options...
rsilverns.sympatico.ca Posted November 21, 2006 Share Posted November 21, 2006 To use java beans is fairly straight forward. Just create either a collection or an array of the bean object and encapsulate that in either a JRBeanArrayDataSource or JRBeanCollectionDataSource. Just make sure the object your encapsulating adheres to the beans standard...1. No public visibility fields2. Properly named getter/setter methods.3. Default no-args c-tor To fill your report, you would have something like... Code: ArrayList al = new ArrayList(); al.add(new SomeBean("SomeParam"«»)); al.add(new SomeBean("SomeParam"«»)); al.add(new SomeBean("SomeParam"«»)); al.add(new SomeBean("SomeParam"«»)); al.add(new SomeBean("SomeParam"«»)); al.add(new SomeBean("SomeParam"«»)); al.add(new SomeBean("SomeParam"«»)); JRBeanCollectionDataSource ds = new JRBeanCollectionDataSource(al); JasperReport jasperReport; JasperPrint jasperPrint; try { jasperReport = JasperCompileManager.compileReport(ROOT_DIR + "myReport.jrxml"«»); Map customParameters = new HashMap(); jasperPrint = JasperFillManager.fillReport( jasperReport, customParameters, ds); JasperExportManager.exportReportToPdfFile( jasperPrint, "c:\test_output.pdf"«»); } catch (JRException e) { e.printStackTrace(); } HTH,Robin Link to comment Share on other sites More sharing options...
rsilverns.sympatico.ca Posted November 21, 2006 Share Posted November 21, 2006 As a follow-up... the fields in your report would be the field names in the bean objects. As well, if you have a sub-report you can pass a collection/array of beans field to the sub-report as its own datasource by using the datasource expression of "new JRBeanCollectionDataSource($F{someBeanList}). HTH,Robin Link to comment Share on other sites More sharing options...
olu_guy Posted November 21, 2006 Author Share Posted November 21, 2006 Thanks robin, I will implement the solution using the guide as provided by you. I dont thnik I will run into problems. That looks to be simple and easy and not complicated as I first thought. I'll write up a manual afterwards so people can benefit from such an example. Cheers. Link to comment Share on other sites More sharing options...
midrag Posted November 22, 2006 Share Posted November 22, 2006 a sample with nested Beans and Subreports can be found here: here. Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now