6 Replies Latest reply on Jul 8, 2011 9:27 AM by v.bannur

    set the custom converter value to the backing bean.

    v.bannur

      Hi,

       

      I have outputText which displays currency value (String). And i have written converter for this component.

      the code is as below.

       

       

      <h:outputText value="#{hotel.displayPrice}" converter="currency_converter">

      </h:outputText>

       

      As soon as the page loads the custom conveter calls and displyas the converted value to the user.

      Everything working fine here. But i need the converted value should set to the backing bean (i.e displayPrice) on click of submit button.

      Is it possible?

        • 1. Re: set the custom converter value to the backing bean.
          mp911de

          Hi,

          you can fetch the field-value by getExternalContext().getRequest().getParameter("....")

          But the idea of the converters is, that you do not need to mess with conversion of data-types in your functions. It's all handled in your converter, the output and the input. When you need to convert your displayPrice back to a double, so do it in your converter. Basically, you need to use the same input- and output-datatype for your bean's properties.

           

          Best regards,

          Mark

          • 2. Re: set the custom converter value to the backing bean.
            v.bannur

            Hi Mark,

             

            Thank you for your promt reply.I have posted my code as below.

             

            When my page loads, initially the displayPrice would be the "100" after convertion it gives "2000" on view.

            This converted value displying on screen, but when I submits, the actual displayPrice is still "100" in backing bean.

            Please let me know how to set this converted value (i.e "2000") to the displayPrice.

             

            The displayPrice of h:outputText is in rich:dataGrid

             

             

            Code:

             

            <rich:dataGrid value="#{hotelSearchResultsBean.searchResults}" binding="#{hotelSearchResultsBean.datagrid}" var="hotel"

                    id="hotelResults"  rowKeyVar="count">

            <h:outputText value="#{hotel.displayPrice}" converter="currency_converter">

            </rich:dataGrid>

             

             

            public class CurrencyConverter implements Converter{

                @Override

                public Object getAsObject(FacesContext context, UIComponent component, String param) {

                    return "2000";

                }

                @Override

                public String getAsString(FacesContext context, UIComponent component, Object param) {    

                    return  "2000";

                }

             

            }

            • 3. Re: set the custom converter value to the backing bean.
              mp911de

              Your converter always returns 2000. Usually it would go (assumed you have a conversion factor of 20 (100 x 20 = 2000)):

               

              public class CurrencyConverter implements Converter{

                  @Override

                  public Object getAsObject(FacesContext context, UIComponent component, String param) {

                        double input = Double.parseDouble(param);

                      return new Double(input / 20);

                  }

                  @Override

                  public String getAsString(FacesContext context, UIComponent component, Object param) {   

                        double input = (Double) param;

                      return  new Double(input * 20).toString();

                  }

               

              }

              • 4. Re: set the custom converter value to the backing bean.
                v.bannur

                Hi Mark,

                 

                My converter is working fine.

                 

                I think I am not able to make you understand properly.

                Let me take an example.

                Assume my bean property(displayPice) original value is "100" before calling converter.

                After converter the value is "2000" and the converted value is (i.e "2000") showing on page properly.

                On submit I am expecting the bean property (displayPice) should give the value as "2000". But the bean property value is still getting "100".

                 

                Please let me know how can I get the Conerted value to the bean property (i.e displayPice) on backing bean.

                • 5. Re: set the custom converter value to the backing bean.
                  mp911de

                  Hi,

                  you get 100 in the property, because that's your back-end value. The front-end value is 2000. When you just want to get, what the user enters in the field, you don't need the currency converter, you would need the <f:convertNumber type="number" integerOnly="true" maxIntegerDigits="2"/> or so. Take a look at http://www.roseindia.net/jsf/jsftraining/using-converter.shtml, there you'll find further information about converters.

                   

                  Best regards,

                  Mark

                  • 6. Re: set the custom converter value to the backing bean.
                    v.bannur

                    Thank you Mark,