You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@myfaces.apache.org by Alan Romaniuc <a....@gmail.com> on 2008/01/22 17:44:49 UTC

Registering a Converter for a "primitive Object"


Hi,

I can not set a converter for the type java.lang.Double. I would like to 
do that once the we use comma to separate decimals (Using a locale based 
converter)
I am using myfaces 1.2.1-snapshot from 17 december, but I tried with 
others too.
I use something like that:
<converter>
        <converter-id>iDoubleConverter</converter-id>
        <converter-for-class>java.lang.Double</converter-for-class>
        
<converter-class>jsf.converter.InternationalDoubleConverter</converter-class>
    </converter>


And my converter:
===============================
public Object getAsObject(FacesContext facesContext, UIComponent 
uiComponent, String value) {
       
        FacesContext fc = FacesContext.getCurrentInstance();
        Locale l = fc.getViewRoot().getLocale();
       
        if (value != null) {
            value = value.trim();
            if (value.length() > 0) {
                try {
                    return 
NumberFormat.getNumberInstance(l).parse(value).doubleValue();
                } catch (ParseException ex) {
                    throw new ConverterException("Bad String " + value + 
" " + l);
                } catch (NumberFormatException e) {
                    throw new ConverterException("Format is not a Number 
for locale" + l);
                }
            }
        }
        return null;
    }

    public String getAsString(FacesContext facesContext, UIComponent 
uiComponent, Object value) {

        if (value == null) {
            return "";
        }
        if (value instanceof String) {
            return (String) value;
        }
        try {
            FacesContext fc = FacesContext.getCurrentInstance();
            Locale l = fc.getViewRoot().getLocale();
            log.debug("Converting Object" + value + " to " + l.toString());
            return NumberFormat.getNumberInstance(l).format(value);
        } catch (Exception e) {
            throw new ConverterException("Format is not a Number");
        }
    }
============

If I use a converter in the component, it will work, showing that the 
converter is working, but I woul like to register it to all Double objects.

Is this a bug from myfaces?









Re: Registering a Converter for a "primitive Object"

Posted by Matthias Wessendorf <ma...@apache.org>.
Trinidad is overriding the "primitive" converters as well.

here is what our built-in faces-cfg does for Double:

   <converter>
      <converter-id>javax.faces.Double</converter-id>
      <converter-class>org.apache.myfaces.trinidadinternal.convert.DoubleConverter</converter-class>
   </converter>

and

   <converter>
      <converter-for-class>java.lang.Double</converter-for-class>
      <converter-class>org.apache.myfaces.trinidadinternal.convert.DoubleConverter</converter-class>
   </converter>

-M
On Jan 22, 2008 10:25 AM, david delbecq <de...@oma.be> wrote:
> As per specifications:
>
> 3.3.3 Standard Converter Implementations
>
>     JSF provides a set of standard Converter implementations. A JSF
>     implementation must
>     register the DateTime and Number converters by name with the
>     Application instance
>     for this web application, as described in the table below. This
>     ensures that the converters are
>     available for subsequent calls to Application.createConverter().
>     Each concrete
>     implementation class must define a static final String constant
>     CONVERTER_ID whose value
>     is the standard converter id under which this Converter is registered.
>     The following converter id values must be registered to create
>     instances of the specified
>     Converter implementation classes:
>     .....
>       javax.faces.Double -- An instance of
>       javax.faces.convert.DoubleConverter (or a subclass of this class).
>
>
>
> I recommend you change your converter declaration to match the
> specification rules. Note: i am not 100% sure it is overridable from
> default behaviour, but this part of section is hopefull:
>
>     A JSF implementation must register converters for all of the
>     following classes using the by-
>     type registration mechanism:
>     ...
>       java.lang.Double, and java.lang.Double.TYPE -- An instance of
>       javax.faces.convert.DoubleConverter (or a subclass of this class).
>
>
>
> Alan Romaniuc a écrit :
>
> >
> >
> > Hi,
> >
> > I can not set a converter for the type java.lang.Double. I would like
> > to do that once the we use comma to separate decimals (Using a locale
> > based converter)
> > I am using myfaces 1.2.1-snapshot from 17 december, but I tried with
> > others too.
> > I use something like that:
> > <converter>
> >        <converter-id>iDoubleConverter</converter-id>
> >        <converter-for-class>java.lang.Double</converter-for-class>
> >
> > <converter-class>jsf.converter.InternationalDoubleConverter</converter-class>
> >
> >    </converter>
> >
> >
> > And my converter:
> > ===============================
> > public Object getAsObject(FacesContext facesContext, UIComponent
> > uiComponent, String value) {
> >              FacesContext fc = FacesContext.getCurrentInstance();
> >        Locale l = fc.getViewRoot().getLocale();
> >              if (value != null) {
> >            value = value.trim();
> >            if (value.length() > 0) {
> >                try {
> >                    return
> > NumberFormat.getNumberInstance(l).parse(value).doubleValue();
> >                } catch (ParseException ex) {
> >                    throw new ConverterException("Bad String " + value
> > + " " + l);
> >                } catch (NumberFormatException e) {
> >                    throw new ConverterException("Format is not a
> > Number for locale" + l);
> >                }
> >            }
> >        }
> >        return null;
> >    }
> >
> >    public String getAsString(FacesContext facesContext, UIComponent
> > uiComponent, Object value) {
> >
> >        if (value == null) {
> >            return "";
> >        }
> >        if (value instanceof String) {
> >            return (String) value;
> >        }
> >        try {
> >            FacesContext fc = FacesContext.getCurrentInstance();
> >            Locale l = fc.getViewRoot().getLocale();
> >            log.debug("Converting Object" + value + " to " +
> > l.toString());
> >            return NumberFormat.getNumberInstance(l).format(value);
> >        } catch (Exception e) {
> >            throw new ConverterException("Format is not a Number");
> >        }
> >    }
> > ============
> >
> > If I use a converter in the component, it will work, showing that the
> > converter is working, but I woul like to register it to all Double
> > objects.
> >
> > Is this a bug from myfaces?
> >
> >
> >
> >
> >
> >
> >
>
>



-- 
Matthias Wessendorf

further stuff:
blog: http://matthiaswessendorf.wordpress.com/
sessions: http://www.slideshare.net/mwessendorf
mail: matzew-at-apache-dot-org

Re: Registering a Converter for a "primitive Object"

Posted by david delbecq <de...@oma.be>.
As per specifications:

3.3.3 Standard Converter Implementations

    JSF provides a set of standard Converter implementations. A JSF
    implementation must
    register the DateTime and Number converters by name with the
    Application instance
    for this web application, as described in the table below. This
    ensures that the converters are
    available for subsequent calls to Application.createConverter().
    Each concrete
    implementation class must define a static final String constant
    CONVERTER_ID whose value
    is the standard converter id under which this Converter is registered.
    The following converter id values must be registered to create
    instances of the specified
    Converter implementation classes:
    .....
      javax.faces.Double -- An instance of
      javax.faces.convert.DoubleConverter (or a subclass of this class).



I recommend you change your converter declaration to match the 
specification rules. Note: i am not 100% sure it is overridable from 
default behaviour, but this part of section is hopefull:

    A JSF implementation must register converters for all of the
    following classes using the by-
    type registration mechanism:
    ...
      java.lang.Double, and java.lang.Double.TYPE -- An instance of
      javax.faces.convert.DoubleConverter (or a subclass of this class).



Alan Romaniuc a écrit :
>
>
> Hi,
>
> I can not set a converter for the type java.lang.Double. I would like 
> to do that once the we use comma to separate decimals (Using a locale 
> based converter)
> I am using myfaces 1.2.1-snapshot from 17 december, but I tried with 
> others too.
> I use something like that:
> <converter>
>        <converter-id>iDoubleConverter</converter-id>
>        <converter-for-class>java.lang.Double</converter-for-class>
>        
> <converter-class>jsf.converter.InternationalDoubleConverter</converter-class> 
>
>    </converter>
>
>
> And my converter:
> ===============================
> public Object getAsObject(FacesContext facesContext, UIComponent 
> uiComponent, String value) {
>              FacesContext fc = FacesContext.getCurrentInstance();
>        Locale l = fc.getViewRoot().getLocale();
>              if (value != null) {
>            value = value.trim();
>            if (value.length() > 0) {
>                try {
>                    return 
> NumberFormat.getNumberInstance(l).parse(value).doubleValue();
>                } catch (ParseException ex) {
>                    throw new ConverterException("Bad String " + value 
> + " " + l);
>                } catch (NumberFormatException e) {
>                    throw new ConverterException("Format is not a 
> Number for locale" + l);
>                }
>            }
>        }
>        return null;
>    }
>
>    public String getAsString(FacesContext facesContext, UIComponent 
> uiComponent, Object value) {
>
>        if (value == null) {
>            return "";
>        }
>        if (value instanceof String) {
>            return (String) value;
>        }
>        try {
>            FacesContext fc = FacesContext.getCurrentInstance();
>            Locale l = fc.getViewRoot().getLocale();
>            log.debug("Converting Object" + value + " to " + 
> l.toString());
>            return NumberFormat.getNumberInstance(l).format(value);
>        } catch (Exception e) {
>            throw new ConverterException("Format is not a Number");
>        }
>    }
> ============
>
> If I use a converter in the component, it will work, showing that the 
> converter is working, but I woul like to register it to all Double 
> objects.
>
> Is this a bug from myfaces?
>
>
>
>
>
>
>