使用Jasper Report 本地化功能, 出现以下问题.
java.util.MissingResourceException: Can't find bundle for base name i18n, locale cs_CZ
at java.util.ResourceBundle.throwMissingResourceException(ResourceBundle.java:837)
at java.util.ResourceBundle.getBundleImpl(ResourceBundle.java:727)
at java.util.ResourceBundle.getBundle(ResourceBundle.java:700)
at net.sf.jasperreports.engine.fill.JRFillDataset.loadResourceBundle(JRFillDataset.java:551)
at net.sf.jasperreports.engine.fill.JRFillDataset.setParameterValues(JRFillDataset.java:582)
at net.sf.jasperreports.engine.fill.JRBaseFiller.setParameters(JRBaseFiller.java:1202)
at net.sf.jasperreports.engine.fill.JRBaseFiller.fill(JRBaseFiller.java:842)
at net.sf.jasperreports.engine.fill.JRBaseFiller.fill(JRBaseFiller.java:814)
at net.sf.jasperreports.engine.fill.JRFiller.fillReport(JRFiller.java:89)
at net.sf.jasperreports.engine.JasperFillManager.fillReport(JasperFillManager.java:601)
at net.sf.jasperreports.engine.JasperFillManager.fillReportToFile
(JasperFillManager.java:492)
at net.sf.jasperreports.engine.JasperFillManager.fillReportToFile
(JasperFillManager.java:448)
at com.tom.i18n.I18nApp.fillReport(I18nApp.java:82)
at com.tom.i18n.I18nApp.main(I18nApp.java:38)
大体上就是说找不到配置文件
在尝试多次后, 发现要在报表模版文件(.jrxml)中的 <jasperReport> 标签的属性 "resourceBundle"中增加
包路径:
<jasperReport
name="I18nReport"
resourceBundle="com.tom.i18n.xxx"
>
这里"com.tom.i18n"是包的路径, "xxx"是配置文件名.
注意这个包的位置与.properties文件的位置没有关系, 是你报表填充的类的所在位置, 因为是在填充报表的时候
使用到了.properties的配置文件
再看看<<jasperReport for java>>, 才明白
The resourceBundle attribute of the <jasperReport> element tells JasperReports
where to get the localized strings to use for the report. In order for this attribute to
work correctly, a property ile with a root name matching the value of the attribute
must exist anywhere in the CLASSPATH when illing the report.
CLassPath 难怪我在模仿例子用ant测试的时候, 不加包也没有错误, 原因是有Ant配置文件中设置了ClassPath