使用JRBeanCollectionDataSource的Jasper Report 6.5.1生成空白页求助
Hey there! Let's figure out why your JasperReports 6.5.1 report is coming up blank even when using JRBeanCollectionDataSource. I've tackled this exact issue plenty of times, so let's walk through the most likely fixes step by step:
Common Causes & Fixes
1. Your Data Source Is Empty or Not Passed Correctly
First, double-check that the list you're feeding into JRBeanCollectionDataSource isn't empty. Even a tiny oversight here will result in a blank report.
- Confirm your bean list has data before initializing the data source:
List<YourBean> beanList = getYourData(); // Make sure this returns non-empty list JRBeanCollectionDataSource dataSource = new JRBeanCollectionDataSource(beanList); - Ensure you're passing the data source to
JasperFillManager.fillReport()as the third parameter, not mixing it up with report parameters.
2. Report Fields Don't Match JavaBean Property Names
JasperReports is strict about matching property names (case-sensitive!). If your bean has a getUserName() method, your report field must be named userName (not Username or USERNAME).
- In your
.jrxml, verify the field expression matches exactly:<field name="userName" class="java.lang.String"/> <!-- The expression in the text field should be $F{userName} --> - Make sure your bean follows standard JavaBean conventions: getters start with
get(e.g.,getUserName()for propertyuserName), no typos allowed.
3. Detail Band Is Missing or Has Zero Height
The Detail Band is where repeated data rows are rendered. If it's set to height 0 or you forgot to add your fields to it, even valid data won't show up.
- Open your report in Jaspersoft Studio:
- Check that the Detail Band has a height greater than 0 (drag its bottom edge to adjust).
- Confirm all your data fields are placed inside the Detail Band (not just Title/Summary bands, which only render once per report).
4. Report Template Isn't Compiled Correctly
A corrupted or uncompiled .jasper file can cause silent failures.
- Compile your
.jrxmlexplicitly in code to catch errors:JasperReport jasperReport = JasperCompileManager.compileReport("path/to/your/report.jrxml"); - Test your template first in Jaspersoft Studio: use the "Preview" tab with sample data. If it shows up blank there too, the issue is in the template, not your Java code.
5. Exceptions Are Being Swallowed
Your code snippet mentions FileNotFoundException—don't ignore other exceptions! JRExceptions often hold the key to what's wrong.
- Wrap your report logic in a try-catch that prints the full stack trace:
try { // Your report generation code here } catch (JRException e) { e.printStackTrace(); // This will reveal missing fields, invalid paths, etc. }
Working Example Code
Here's a complete, minimal example to compare against your implementation:
import net.sf.jasperreports.engine.*; import net.sf.jasperreports.engine.data.JRBeanCollectionDataSource; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; public class ReportGenerator { public static void main(String[] args) { try { // 1. Populate test data List<User> users = new ArrayList<>(); users.add(new User("Freddie Wilson", "freddie@example.com")); users.add(new User("Jane Doe", "jane@example.com")); // 2. Initialize data source JRBeanCollectionDataSource dataSource = new JRBeanCollectionDataSource(users); // 3. Compile report template JasperReport report = JasperCompileManager.compileReport("user-report.jrxml"); // 4. Set report parameters (if any) Map<String, Object> params = new HashMap<>(); params.put("ReportTitle", "User List"); // 5. Fill report JasperPrint print = JasperFillManager.fillReport(report, params, dataSource); // 6. Export to PDF JasperExportManager.exportReportToPdfFile(print, "user-report.pdf"); System.out.println("Report generated successfully!"); } catch (JRException e) { e.printStackTrace(); } } // Sample JavaBean public static class User { private String name; private String email; public User(String name, String email) { this.name = name; this.email = email; } // Getters (must match report field names) public String getName() { return name; } public String getEmail() { return email; } } }
Start with these checks—9 times out of 10, the blank report issue boils down to one of these points. Let me know if you hit any specific errors after debugging!
内容的提问来源于stack exchange,提问作者Freddie Wilson

