1

我一直在使用从数据库加载的 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;
        }            
    }
4

1 回答 1

1

这是因为我的资源包只是一个 POJO 而不是托管 bean,我无法使用@EJB. 我不得不使用 JNDI 来查找它。这是代码:

try {            
            Context ctx = new InitialContext();            
            ArticleLclFacade arBean = (ArticleLclFacade) ctx.lookup("java:global/gtest/ArticleLclFacade");
            List<ArticleLcl> articles = arBean.getArticles(language);
            for(Iterator<ArticleLcl> it = articles.iterator(); it.hasNext();){
                ArticleLcl article = it.next();
                messages.put(article.getArId().getArId().toString(), article.getArTitle());                    
            }                    

        } catch (NamingException ex) {
            Logger.getLogger(test.class.getName()).log(Level.SEVERE, null, ex);
        }      
于 2013-11-09T09:52:03.800 回答