4 Replies Latest reply on Apr 1, 2007 12:23 PM by Hannes Schulz

    Question on Converter

    Hannes Schulz Newbie

      Hello,

      i defined a converter in faces-config.xml:

      <converter>
       <converter-id>MyEntityConverter</converter-id>
       <converter-class>webapp.converter.MyEntityConverter</converter-class>
      </converter>


      and in the view i called it via:

      <h:selectOneMenu converter="#{MyEntityConverter}">
       <s:selectItems value="#{itemsmanager.entities}" />
      </h:selectOneMenu>


      but it doesnt seem to use the converter.
      Renaming the converter-id to something like "blabla" doesnt throw an error when the page is rendered. I also tried to reference it via "converter-for-class" with no effect.

      Does defining Converters in faces-config.xml not work with seam or is there an error in the code?

      I use JBoss-4.0.5.GA
      with seam 1.2.0.Patch1
      with jsf 1.2 and facelets


      Thanks for help,

      hasc

        • 1. Re: Question on Converter
          fhh Expert

          Why is #{MyEntityConverter} an EL expression? This will (most likely) come out as null. So you are not setting a converter at all.

          Regards

          Felix

          • 2. Re: Question on Converter
            Hannes Schulz Newbie

            right. this was from a try to bind the converter via a getter method like in the dvd example.

            i also tried it with:

            <h:inputText id="EffectiveArea" value="#{calculator.area}">
             <f:converter converterId="AreaConverter"/>
            </h:inputText>


            or...

            <h:inputText id="EffectiveArea" value="#{calculator.area}" converter="AreaConverter"/>


            in faces-config there's:
            <converter>
             <converter-id>AreaConverter</converter-id>
             <converter-class>my.webapp.converter.AreaConverter</converter-class>
            </converter>


            • 3. Re: Question on Converter
              Hannes Schulz Newbie

              sorry i was wrong. it seems to use the converter.

              i have a question regarding the converter.

              #{calculator.area}


              the expression references a property which is defined here:

              @Stateful
              class CalculatorBean implements Calculator
              
              float area;
              public void setArea(float area)
              {
               this.area = area;
              }
              
              public float getArea()
              {
               return area;
              }
              
              public void calculate(){do something}
              


              now what i want is, that if the property area is not set, the form field should be empty.

              if no converter is specified #{calculator.area} is rendered to "0.0".

              I tried the following in the toString method of the AreaConverter class:
              public String getAsString(FacesContext facesContext, UIComponent component, Object obj)
               {
               if(obj == null || obj.equals(0.0))
               {
               String str = "";
               return str;
               }
              
               return obj.toString();
               }
              


              but it still returns "0.0"

              has somone a hint why this happens?



              • 4. Re: Question on Converter
                Hannes Schulz Newbie

                 

                public String getAsString(FacesContext facesContext, UIComponent component, Object obj)
                {
                 Float val = (Float) obj;
                
                 if(val == null || val == 0)
                 {
                 String str = "";
                 return str;
                 }
                 return obj.toString();
                }
                

                solved this question...