Pregunta

I've been using the method described in Stack Over Flow question internationalization in JSF with ResourceBundle entries which are loaded from database. The problem I'm having is with JPA in the overridden newBundle() method in DBControl class. When I use a named query to fill the Map<String,String> it gives a missingResourceException. I then tried to build a resource class that extends ListResourceBundle inside the DBControl class and do the JPA there but it again throws the same exception. Below is my code for more clarification:

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;
        }            
    }
¿Fue útil?

Solución

It was because my resource bundle was only a POJO and not a managed bean and I could not inject using @EJB. I had to look it up using JNDI. Here is the code:

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);
        }      
Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top