我一直在使用从数据库加载的 ResourceBundle 条目在 JSF 中的 Stack Over Flow 问题国际化中描述的方法。我遇到的问题是newBundle()
在类中的重写方法中使用 JPA DBControl
。当我使用命名查询来填充Map<String,String>
它时,它会给出一个missingResourceException
. 然后我尝试构建一个在类ListResourceBundle
内部扩展的资源类DBControl
并在那里执行 JPA,但它再次抛出相同的异常。以下是我的代码以进行更多说明:
protected class DBControl extends Control{
@Override
public ResourceBundle newBundle
(String baseName, Locale locale, String format, ClassLoader loader, boolean reload)
throws IllegalAccessException, InstantiationException, IOException
{
return new ArticleResources(locale);
}
protected class ArticleResources extends ListResourceBundle{
private Locale locale;
public ArticleResources (Locale locale){
this.locale = locale;
}
String language = locale.getLanguage();
@Override
protected Object[][] getContents(){
TypedQuery<ArticleLcl> query = em.createNamedQuery("ArticleLcl.findForLocale", ArticleLcl.class);
query.setParameter("lang", language);
List<ArticleLcl> articles = query.getResultList();
Object[][] allArticles = new Object[articles.size()][3];
int i = 0;
for(Iterator<ArticleLcl> it = articles.iterator(); it.hasNext();){
ArticleLcl article = it.next();
allArticles[i] = new Object[]{article.getArticleId().getArticleId().toString(),article.getArticleTitle()};
messages.put(article.getArticleId().getArticleId().toString(),article.getArticleTitle());
i++;
}
return allArticles;
}
}