3 Replies Latest reply on Aug 13, 2008 8:58 PM by Mikael Andersson

    Converter Problems (one-way two-way)

    Rene Felgenträger Newbie

      Hi folks,


      i hope somebody in the forum can help me. I get an exception or validation error and neither I understand the reason nor i dont know what framework (Seam, jsf, hibernate) causes the exception. This is my code in the frontend:



      <s:decorate template="/layout/edit3.xhtml">
       <ui:define name="label">Startperiode:</ui:define>
       <h:selectOneMenu value="#{quotationPoolAction.validFrom}" 
           requiredMessage="Bitte Startperiode auswählen"  required="true">                                                           
            <s:selectItems var="date" value="#{quotationPeriods}"
                 <label="#{date.label}" itemValue="#{date.label}" />
            <f:converter converterId="quotationPeriodConverter" />
       </h:selectOneMenu>
      </s:decorate>
      



      From my conversational scoped backing bean i get a list of objects - lets say quotationPeriods. The list contains pretty complex objects (QuotationPeriod), nethertheless I am only interested in one particular property which itself is a date object. The textual representation of this date is covered by the


      #{date.label}



      method and corresponds to the yyyy-MM-dd pattern. Coding from the backing bean to the frontend works fine.
      But the coding from the frontend to backing bean fails and an invalid value - Faces-Message occurs in the frontend .
      The Converter itself does its job as expected. It tries to create a normal date object from the text-string.




      As far as I see is the date object never applied to the backing bean


      #{quotationPoolAction.validFrom}



      due to the invalid value Exception.


      Why is this value invalid???? Is it not possible to convert from one complex object (QuotationPeriod) to another object (Date)?
      If the validFrom is coded as String and I dont use the converter everything works, too. But I dislike the fact that the conversation from String to object is then done in the backing bean and not by the converter!



      Thx for your help in advance!



      Greetings

        • 1. Re: Converter Problems (one-way two-way)
          Mikael Andersson Master

          Can you post the code for the converter, might be able to spot something there.
          If there is a stack trace please post that as well.


          Have you debugged your converter to make sure that it creates the objects properly?


          -micke

          • 2. Re: Converter Problems (one-way two-way)
            Rene Felgenträger Newbie

            public class QuotationPeriodConverter implements Converter {
            
                public Object getAsObject(FacesContext context, UIComponent component, String obj) {
                    obj = obj.trim();
                    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
                    
                    try {            
                        
                        Date date = sdf.parse(obj);
                        System.out.println("Converted Date: " + date);
                        return date;
                    } catch (ParseException e) {
                        e.printStackTrace();
                        throw new ConverterException("Parsen von " + obj + " schlug fehl");
                        
                    }
                    
                }
            
                public String getAsString(FacesContext context, UIComponent component, Object obj) {
                    
                    if (true) return obj.toString();
                    try {
                        String string =  DateUtils.format(((QuotationPeriod)obj).getBeginDate());
                        System.out.print("String: " + string);
                        return string;
                    }
                    catch(Exception e) {
                        e.printStackTrace();
                        throw new ConverterException("String-Umwandlung fehlgeschlagen");
                    }
                    
                }



            the code works fine, as far as i see. Note that i convert from QuotationPeriod (getAsString) to Date(getAsObject)!


            greetings

            • 3. Re: Converter Problems (one-way two-way)
              Mikael Andersson Master

              Hi


              Only used converters with simple MyObj to-and-from String mapping (not MyObj to String to Date).


              On vacation now so I can't experiment, but I'd suggest debugging and looking at the types of the converters parameters.


              - micke