7 Replies Latest reply on Sep 12, 2007 12:03 PM by stephen.friedrich

    Converter behavior  PROBLEM

    Uros Milivojevic Newbie

      Hi,

      I'm having problem with jsf converter behavior.
      This is converter code:

      public class OfferPriceConvertor implements Converter{
      
       public Object getAsObject(FacesContext arg0, UIComponent arg1, String string) {
       BigDecimal price = null;
       try {
       price = new BigDecimal( string );
       } catch (Exception e) {
       price = new BigDecimal(0);
       }
       return price;
       }
      
       public String getAsString(FacesContext arg0, UIComponent arg1, Object object) {
       BigDecimal price = (BigDecimal) object;
       BigDecimal minPrice = null;
       try {
       minPrice = new BigDecimal( GlobalProperties.getStaticData("minimum_offer_price") );
       } catch (Exception e) {
       minPrice = new BigDecimal(0);
       }
       return ((price != null && price.doubleValue() > 0) ? price.toPlainString() : minPrice.toPlainString());
       }
      
      }


      Converter works ok when page is rendered.
      Problem appears when page is submitted. Function "getAsObject" is executed BUT instead of entering action method I got same page displayed again.
      There are probably some problem inside jsf lifecycle but I don't see anything in console.

      These anyone had same problem?
      I am interested in any suggestion.

      Thanks,
      Uros.

        • 1. Re: Converter behavior  PROBLEM
          stephen.friedrich Novice

          First assumption whenever the same page gets redisplayed:
          Validation failed and your page is missing h:message(s) tags to actually display the validation error messages.

          • 2. Re: Converter behavior  PROBLEM
            Uros Milivojevic Newbie

            Hi Stephen,

            as "validation" are you referring to explicitly added jsf validation, because I don't use that?

            This is html code:

            <h:inputText id="price" value="#{offerBean.offer.price}" converter="offerPriceConvertor" class="inputText"></h:inputText>

            I think is important to add that set-er for "offerBean.offer.price" is never executed. After "getAsObject" it's going directly to "getAsString" - by directly I mean it skips my code not the code for handling jsf lifecycle.

            Does jsf controller checks object returned by "getAsObject" method? Should't it just call set-er and pass argument returned by "getAsObject" method?
            Can someone explain what actually happens behind the scene?

            Thanks,
            Uros.

            • 3. Re: Converter behavior  PROBLEM
              stephen.friedrich Novice

              Well have you simply tried to add a single h:messages tag to your page? Let's see if you get any messages.

              For example when you use hibernate validation annotations on your entity beans, then seam will add matching jsf validators by itself.

              The behaviour you describe is consistent with that: The string object is converted, but validation fails, so it is never transfered to the model.

              I am no expert in JSF internals eithers. I am just working my way through "Core Java Server Faces, 2nd edition" and I can really recommend it.

              • 4. Re: Converter behavior  PROBLEM
                Uros Milivojevic Newbie

                Hi,

                it was validation error. I got ""price": Conversion error occurred." from h:messages.

                BUT why is validation happening?
                I even changed "getAsObject" method body to:

                public Object getAsObject(FacesContext arg0, UIComponent arg1, String string) {
                 return new BigDecimal(0);
                 }

                and I got same error.
                Is this some BUG is JSF or Seam or I am doing something wrong?

                Thanks,
                Uros!

                • 5. Re: Converter behavior  PROBLEM
                  stephen.friedrich Novice

                  Hm, let's see how your "offer" bean and especially the "price" property is defined.

                  • 6. Re: Converter behavior  PROBLEM
                    Uros Milivojevic Newbie

                    Hi Stephen,

                    this is offer without irrelevant information:

                    public class Offer implements Serializable, Cloneable {
                    
                     private BigDecimal price;
                    
                     public BigDecimal getPrice() {
                     return price;
                     }
                    
                     public void setPrice(BigDecimal price) {
                     this.price = price;
                     }
                    }
                    


                    Important to say is that there are situation where I use converter for java.util.List objects and everything work fine.

                    Thanks,
                    Uros.

                    • 7. Re: Converter behavior  PROBLEM
                      stephen.friedrich Novice

                      Ok, I am more or less at the end of my wisdom :-(

                      Maybe you'll get more info out of h:messages by adding attribute showDetail="true".

                      Else I would put a breakpoint on FacesContext.addMessage() and try to figure it out from the call stack.