简体   繁体   中英

Generate jasper report using singe java bean

i have requirement to create order declaration report, i am using jasper studio to create the jasper template. in that template i have order id, customer details and his address, with these details i have to create a report.

i have below jasper report code

    JasperCompileManager.compileReportToFile("src/main/resource/orderDeclarationForm.jrxml");
    JasperPrint jasperPrint = JasperFillManager.fillReport("src/main/resource/orderDeclarationForm.jasper", new HashMap<String, Object>(), new JRTableModelDataSource(getTableModelData()));
    // JasperExportManager.exportReportToPdfFile("resource/orderDeclarationForm.jrprint");
    JasperExportManager.exportReportToPdfFile(jasperPrint, "src/main/resource/orderDeclarationForm.pdf");

but instead of JRTableModelDataSource i have to pass java bean class so the jasper engine has to take the data from one single java bean, i have gone through javabean as datasource where it takes list of beans, but my requirement is only one bean which has order details. please advice me on this

If you only need one instance of a bean to be passed to JasperFillManager.fillReport method, then it makes sense to pass them as parameters as long as their count is feasible (in your case its only 3).

    Map<String,Object> params = new HashMap<String,Object>();
    params.put("orderId", xxx);
    params.put("customerDetails", xxx);
    params.put("address", xxx);

Afterwards, pass this params Map object:

JasperPrint jasperPrint = JasperFillManager.fillReport("src/main/resource/orderDeclarationForm.jasper", params, new JRTableModelDataSource(getTableModelData()));

Please check this link for more info on how to read Parameters from within your .jrxml file.

Thanks.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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