6 Replies Latest reply on Mar 11, 2013 9:18 AM by kenfinni

    Custom JSF Converter & CDI

    mbasovni

      Hello,

       

      could somebody help me to solve problem how to inject EntityManager (or another DAO class) in "object converter class" which implements interface javax.faces.convert.Converter?

       

      My converter CalendarConverter looks like:

       

      import javax.faces.component.UIComponent;
      import javax.faces.context.FacesContext;
      import javax.faces.convert.Converter;
      import javax.faces.convert.FacesConverter;
      import javax.inject.Inject;
      import javax.persistence.EntityManager;
      
      
      @FacesConverter(forClass=Calendar.class)
      public class CalendarConverter implements Converter {
      
          @Inject
          private EntityManager em;
      
          @Override
          public Object getAsObject(FacesContext context, UIComponent component,
              String value) {
      
              return em.find(Calendar.class, Integer.parseInt(value));
          }
      
          @Override
          public String getAsString(FacesContext context, UIComponent component,
                  Object value) {
      
              return String.valueOf(((Calendar) value).getId());
      
          }
      
      }
      

       

      And I use here:

       

             

          

              <h:selectOneMenu value="#{eventController.event.calendar}" id="calendar">
                  <f:selectItems value="#{calendarController.getAllCalendars()}" var="calendarItem" 
                      itemValue="#{calendarItem}" itemLabel="#{calendarItem.name}" /> 
      
              </h:selectOneMenu>
      

       

      I have read really a lot of articles how to try to solve it, but no solution.

       

      e.g.

      - I tried to add @Named

       

      - I tried to find EntiityManager by function lookup

      https://community.jboss.org/message/652470#652470

      - Adding of @PersistenceContext

      http://stackoverflow.com/questions/13182660/conversion-error-setting-value-52-for-null-converter

      - somebody used something like this

      https://community.jboss.org/thread/178777

       

      Also this article was helpful:

      http://stackoverflow.com/questions/4734580/jsf-2-selection-combo-box-with-without-conversion-strategy/4735322#4735322

       

      Has somebody any idea?

        • 1. Re: Custom JSF Converter & CDI
          kenfinni

          Martin,

           

          I'm confused about what the problem is.

           

          Are you getting an exception of some kind?

           

          Or is the code you have not doing anything?

           

          Ken

          • 2. Re: Custom JSF Converter & CDI
            mbasovni

            When I have for my CalendarConverter only one annotation:

             

            @FacesConverter(forClass=Calendar.class)
            

             

            and I inject EntityManager just like this:

             

            @Inject
            private EntityManager em;
            

             

            I get NullPointerException. Look at file "log1".

             

            When I tried to add:

             

            @Named
            

             

            => the same exception

             

            Gatein: 3.5.2.Final

            Portletbridge: 3.2.0.Beta1

            JBoss AS: 7.1.3.Final

            cdi-portlet-integration: 1.0.0.Alpha2

            • 3. Re: Custom JSF Converter & CDI
              kenfinni

              Martin,

               

              After looking at http://stackoverflow.com/questions/13182660/conversion-error-setting-value-52-for-null-converter did you add @ManagedBean onto your converter?

               

              As BalusC points out in his response, without @ManagedBean the EntityManager will not be injected.  Also, the EntityManager would need to use @PersistenceContext instead of @Inject, unless you had a CDI producer creating the EntityManager that could then be injected.

               

              Ken

              • 4. Re: Custom JSF Converter & CDI
                mbasovni

                Ken,

                 

                look at it. It does not work!

                 

                package dip.xbasov00.calendar.util;
                
                import javax.faces.bean.ManagedBean;
                import javax.faces.component.UIComponent;
                import javax.faces.context.FacesContext;
                import javax.faces.convert.Converter;
                import javax.faces.convert.FacesConverter;
                import javax.persistence.EntityManager;
                import javax.persistence.PersistenceContext;
                
                import dip.xbasov00.calendar.domain.Calendar;
                
                @ManagedBean
                @FacesConverter(forClass=Calendar.class)
                public class CalendarConverter implements Converter {
                
                    @PersistenceContext
                    private EntityManager em;
                
                    @Override
                    public Object getAsObject(FacesContext context, UIComponent component,
                        String value) {
                
                        System.out.println("CalendarConverter.getAsObject: " + value);
                        if (em == null)
                            System.out.println("EntityManager is NULL !!!!");
                        return em.find(Calendar.class, Integer.parseInt(value));
                    }
                
                    @Override
                    public String getAsString(FacesContext context, UIComponent component,
                            Object value) {
                
                        System.out.println("CalendarConverter.getAsString: " + value.toString());
                        return String.valueOf(((Calendar) value).getId());
                
                    }
                }
                

                 

                NullPointerException again... :-(

                 

                Martin

                • 5. Re: Custom JSF Converter & CDI
                  mbasovni

                  Problem is resolved:

                   

                  @FacesConverter(value="calendarConverter", forClass=Calendar.class)
                  public class CalendarConverter implements Converter {
                  
                      private CalendarDAO calendarDao;
                  
                      public CalendarConverter() {
                          super();
                          try {
                              InitialContext ic = new InitialContext();
                              calendarDao = (CalendarDAO) ic.lookup("java:module/EJBCalendarDAO");
                             } catch (NamingException e) {
                              e.printStackTrace();
                         }
                      }
                  
                      @Override
                      public Object getAsObject(FacesContext context, UIComponent component,
                          String value) {
                  
                          Calendar c = calendarDao.getCalendar(Integer.parseInt(value));
                          return c;
                      }
                  
                      @Override
                      public String getAsString(FacesContext context, UIComponent component,
                              Object value) {
                  
                          return String.valueOf(((Calendar) value).getId());
                  
                      }
                  }
                  

                   

                  Calendar class must override metod equals and hashCode!

                  • 6. Re: Custom JSF Converter & CDI
                    kenfinni

                    Martin,

                     

                    Glad you were able to figure out the problem.

                     

                    Ken