4 Replies Latest reply on Sep 11, 2007 1:34 PM by nebukadnezzar

    Custom JSF Converter???

    nebukadnezzar

      Hi!

      I'm trying to write a custom converter for passing my objects as GET-parameter...
      But I still can't get them to work!

      has anybody an example of doing this???

      here's my code:

      @Name("categoryConverter")
      @Stateless
      public class CategoryConverterBean implements javax.faces.convert.Converter, CategoryConverter {
      
       @PersistenceContext
       private EntityManager entityManager;
      
       public Object getAsObject(FacesContext arg0, UIComponent arg1, String str)
       throws ConverterException {
       try {
       long id = Long.parseLong(str);
      
       return entityManager.createQuery("Select c from Category where c.id = :id")
       .setParameter("id", id)
       .getSingleResult();
      
       } catch (Exception e) {
       throw new ConverterException();
       }
      
       }
      
       public String getAsString(FacesContext arg0, UIComponent arg1, Object obj)
       throws ConverterException {
       if (obj instanceof Category) {
       return Long.toString(((Category)obj).getId());
       } else
       throw new ConverterException();
       }
      
      
      }
      


        • 1. Re: Custom JSF Converter???
          pmuir

          Try using @Converter.

          • 2. Re: Custom JSF Converter???
            nebukadnezzar

             

            "pete.muir@jboss.org" wrote:
            Try using @Converter.


            doesn't work...

            .page.xml:
            ...
            <param name="category" value="#{appointmentList.appointment.category}" converter="#{appointmentCategoryConverter}" />
            ...


            .xhtml
            ...
            <h:selectOneMenu id="category" value="#{appointmentList.appointment.category}" >
             <s:selectItems value="#{appointmentCategoryList.resultList}" var="c" label="#{c.name}" noSelectionLabel="" />
             <s:convertEntity/>
            </h:selectOneMenu>
            ...


            .java
            @Converter
            @Name("appointmentCategoryConverter")
            @Stateless
            public class AppointmentCategoryConverterBean implements javax.faces.convert.Converter, AppointmentCategoryConverter {
            
             @PersistenceContext
             private EntityManager entityManager;
            
             public Object getAsObject(FacesContext arg0, UIComponent arg1, String str)
             throws ConverterException {
             try {
             long id = Long.parseLong(str);
            
             return entityManager.createQuery("Select ac from AppointmentCategory where ac.id = :id")
             .setParameter("id", id)
             .getSingleResult();
            
             } catch (Exception e) {
             throw new ConverterException(e);
             }
            
             }
            
             public String getAsString(FacesContext arg0, UIComponent arg1, Object obj)
             throws ConverterException {
             try {
             return Long.toString(((AppointmentCategory)obj).getId());
             } catch (Exception e) {
             throw new ConverterException(e);
             }
             }
            }


            • 3. Re: Custom JSF Converter???
              pmuir

               

              <param name="category" value="#{appointmentList.appointment.category}" converter="appointmentCategoryConverter" />


              • 4. Re: Custom JSF Converter???
                nebukadnezzar

                nothing happens.... no param no exception nothing....

                maybe i should just pass around the id as Long or is there any other approach?