2013-03-11 62 views
4

jasper Reports在運行時是否可能加載ResourceBundle(對於i18n)?如何在運行時加載jasperreports資源包?

我想創建從JRXML文件(例如c:\reports\report.jrxml
與我的屬性標籤文件位於(c:\messages\report.properties)的報告。

我只找到屬性文件在類加載器中的示例。

感謝

回答

5

John Ferguson's blog提到的伎倆是使用自定義資源包實例來覆蓋REPORT_RESOURCE_BUNDLE參數。

// Compiling the report is not a necessary step; prefer using .jasper files 
// that have been pre-compiled to avoid this compilation step. 
// 
JasperDesign jasperDesign = JasperManager.loadXmlDesign("Report.jrxml"); 
JasperReport jasperReport = JasperManager.compileReport(jasperDesign); 

Map parameters = new HashMap(); 
parameters.put("REPORT_LOCALE",LocaleManager.currentLocale()); 
parameters.put("REPORT_RESOURCE_BUNDLE",resourceBundle); 
Connection conn = DBConnectionFactory.getConnection(); 
JasperPrint jasperPrint = JasperFillManager.fillReport(jasperReport, 
                 parameters, 
                 conn); 

resourceBundle可以來自任何地方。例如:從資源包

try(FileInputStream fis = new FileInputStream("/tmp/report.properties")) { 
    ResourceBundle resourceBundle = new PropertyResourceBundle(fis); 

    // Pass resourceBundle into the report, as shown above. 
} 
+0

爲了模擬資源束繼承,請參閱:https://stackoverflow.com/a /59087分之4615268 – 2017-10-27 16:21:50

2

裝包:

ResourceBundle bundle=ResourceBundle.getBundle("/reports/bundles/bundle",Locale.CANADA_FRENCH); 

填充報告::

params.put("REPORT_RESOURCE_BUNDLE", bundle); 
JasperPrint jasPrint = JasperFillManager.fillReport(reportStream, params, data);