You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@myfaces.apache.org by Jason Nesbitt <ja...@gmail.com> on 2005/09/23 16:29:51 UTC

conversion error

I have an input text field that is bound to a long value in the backing
bean. I'm using a custom converter because I want the initial value of the
input text field to be the empty string and because I want to provide an
error message that is more specific. However, when I enter a value in the
text field that is not a valid long I get the following error.
ERROR org.apache.myfaces.el.ValueBindingImpl - Cannot set value for
expression '#{login.adId}' to a new value of type
javax.faces.convert.ConverterException
javax.servlet.jsp.el.ELException: Attempt to coerce a value of type "
javax.faces.convert.ConverterException" to type "java.lang.Long"

What am I doing wrong here? My input text declaration is

<h:inputText size="12" maxlength="25" value="#{login.adId}" converter="#{
login.adIdConverter}" />

and my converter property is

public Converter getAdIdConverter() {
return new Converter() {
public Object getAsObject(
FacesContext facesContext,
UIComponent uiComponent,
String s) throws ConverterException {
try {
return new Long(s);
} catch (NumberFormatException e) {
return new ConverterException("Invalid Ad Id");
}
}

public String getAsString(
FacesContext facesContext,
UIComponent uiComponent,
Object o) throws ConverterException {
Long adId = (Long) o;

if (adId == null || adId.longValue() == 0) {
return "";
} else {
return String.valueOf(adId);
}
}
};
}



Thanks

Jason

Re: conversion error

Posted by Martin Marinschek <ma...@gmail.com>.
Huhh? The string value needs to be passed to the getAsObject method...


regards,

Martin

On 9/26/05, ::SammyRulez:: <sa...@gmail.com> wrote:
> I'm also experiencing problems with a converter very similar to the
> one posted by Jason.
>
> In my speculation it seems to me that the string value is passed to
> the getAsObject method insted of getAsString method...
>
> 2005/9/23, Sean Schofield <se...@gmail.com>:
> > Also see the brand new bug I filed (MYFACES-611) earlier today.  Even
> > with Mike's suggestion your ConverterExceptions may not work as
> > expected b/c of this.
> >
> > sean
> >
> > On 9/23/05, Mike Kienenberger <mk...@gmail.com> wrote:
> > > Instead of
> > >
> > > return new ConverterException("Invalid Ad Id");
> > >
> > > you need to use
> > >
> > > throw new ConverterException("Invalid Ad Id");
> > >
> > > On 9/23/05, Jason Nesbitt <ja...@gmail.com> wrote:
> > > > I have an input text field that is bound to a long value in the backing
> > > > bean.  I'm using a custom converter because I want the initial value of the
> > > > input text field to be the empty string and because I want to provide an
> > > > error message that is more specific.  However, when I enter a value in the
> > > > text field that is not a valid long I get the following error.
> > > >  ERROR org.apache.myfaces.el.ValueBindingImpl  - Cannot set
> > > > value for expression '#{login.adId}' to a new value of type
> > > > javax.faces.convert.ConverterException
> > > >  javax.servlet.jsp.el.ELException: Attempt to coerce a
> > > > value of type "javax.faces.convert.ConverterException" to
> > > > type "java.lang.Long"
> > > >
> > > >  What am I doing wrong here? My input text declaration is
> > > >
> > > >  <h:inputText size="12" maxlength="25" value="#{login.adId}"
> > > > converter="#{login.adIdConverter}" />
> > > >
> > > >  and my converter property is
> > > >
> > > >  public Converter getAdIdConverter() {
> > > >          return new Converter() {
> > > >                  public Object getAsObject(
> > > >                      FacesContext facesContext,
> > > >                      UIComponent uiComponent,
> > > >                      String s) throws ConverterException {
> > > >                      try {
> > > >                          return new Long(s);
> > > >                      } catch (NumberFormatException e) {
> > > >                          return new ConverterException("Invalid Ad Id");
> > > >                      }
> > > >                  }
> > > >
> > > >                  public String getAsString(
> > > >                      FacesContext facesContext,
> > > >                      UIComponent uiComponent,
> > > >                      Object o) throws ConverterException {
> > > >                      Long adId = (Long) o;
> > > >
> > > >                      if (adId == null || adId.longValue() == 0) {
> > > >                          return "";
> > > >                      } else {
> > > >                          return String.valueOf(adId);
> > > >                      }
> > > >                  }
> > > >              };
> > > >      }
> > > >
> > > >
> > > >
> > > >  Thanks
> > > >
> > > >  Jason
> > > >
> > >
> >
>
>
> --
> ::SammyRulez::
> http://sammyprojectz.blogspot.com
>


--

http://www.irian.at
Your JSF powerhouse -
JSF Trainings in English and German

Re: conversion error

Posted by "::SammyRulez::" <sa...@gmail.com>.
I'm also experiencing problems with a converter very similar to the
one posted by Jason.

In my speculation it seems to me that the string value is passed to
the getAsObject method insted of getAsString method...

2005/9/23, Sean Schofield <se...@gmail.com>:
> Also see the brand new bug I filed (MYFACES-611) earlier today.  Even
> with Mike's suggestion your ConverterExceptions may not work as
> expected b/c of this.
>
> sean
>
> On 9/23/05, Mike Kienenberger <mk...@gmail.com> wrote:
> > Instead of
> >
> > return new ConverterException("Invalid Ad Id");
> >
> > you need to use
> >
> > throw new ConverterException("Invalid Ad Id");
> >
> > On 9/23/05, Jason Nesbitt <ja...@gmail.com> wrote:
> > > I have an input text field that is bound to a long value in the backing
> > > bean.  I'm using a custom converter because I want the initial value of the
> > > input text field to be the empty string and because I want to provide an
> > > error message that is more specific.  However, when I enter a value in the
> > > text field that is not a valid long I get the following error.
> > >  ERROR org.apache.myfaces.el.ValueBindingImpl  - Cannot set
> > > value for expression '#{login.adId}' to a new value of type
> > > javax.faces.convert.ConverterException
> > >  javax.servlet.jsp.el.ELException: Attempt to coerce a
> > > value of type "javax.faces.convert.ConverterException" to
> > > type "java.lang.Long"
> > >
> > >  What am I doing wrong here? My input text declaration is
> > >
> > >  <h:inputText size="12" maxlength="25" value="#{login.adId}"
> > > converter="#{login.adIdConverter}" />
> > >
> > >  and my converter property is
> > >
> > >  public Converter getAdIdConverter() {
> > >          return new Converter() {
> > >                  public Object getAsObject(
> > >                      FacesContext facesContext,
> > >                      UIComponent uiComponent,
> > >                      String s) throws ConverterException {
> > >                      try {
> > >                          return new Long(s);
> > >                      } catch (NumberFormatException e) {
> > >                          return new ConverterException("Invalid Ad Id");
> > >                      }
> > >                  }
> > >
> > >                  public String getAsString(
> > >                      FacesContext facesContext,
> > >                      UIComponent uiComponent,
> > >                      Object o) throws ConverterException {
> > >                      Long adId = (Long) o;
> > >
> > >                      if (adId == null || adId.longValue() == 0) {
> > >                          return "";
> > >                      } else {
> > >                          return String.valueOf(adId);
> > >                      }
> > >                  }
> > >              };
> > >      }
> > >
> > >
> > >
> > >  Thanks
> > >
> > >  Jason
> > >
> >
>


--
::SammyRulez::
http://sammyprojectz.blogspot.com

Re: conversion error

Posted by Sean Schofield <se...@gmail.com>.
Also see the brand new bug I filed (MYFACES-611) earlier today.  Even
with Mike's suggestion your ConverterExceptions may not work as
expected b/c of this.

sean

On 9/23/05, Mike Kienenberger <mk...@gmail.com> wrote:
> Instead of
>
> return new ConverterException("Invalid Ad Id");
>
> you need to use
>
> throw new ConverterException("Invalid Ad Id");
>
> On 9/23/05, Jason Nesbitt <ja...@gmail.com> wrote:
> > I have an input text field that is bound to a long value in the backing
> > bean.  I'm using a custom converter because I want the initial value of the
> > input text field to be the empty string and because I want to provide an
> > error message that is more specific.  However, when I enter a value in the
> > text field that is not a valid long I get the following error.
> >  ERROR org.apache.myfaces.el.ValueBindingImpl  - Cannot set
> > value for expression '#{login.adId}' to a new value of type
> > javax.faces.convert.ConverterException
> >  javax.servlet.jsp.el.ELException: Attempt to coerce a
> > value of type "javax.faces.convert.ConverterException" to
> > type "java.lang.Long"
> >
> >  What am I doing wrong here? My input text declaration is
> >
> >  <h:inputText size="12" maxlength="25" value="#{login.adId}"
> > converter="#{login.adIdConverter}" />
> >
> >  and my converter property is
> >
> >  public Converter getAdIdConverter() {
> >          return new Converter() {
> >                  public Object getAsObject(
> >                      FacesContext facesContext,
> >                      UIComponent uiComponent,
> >                      String s) throws ConverterException {
> >                      try {
> >                          return new Long(s);
> >                      } catch (NumberFormatException e) {
> >                          return new ConverterException("Invalid Ad Id");
> >                      }
> >                  }
> >
> >                  public String getAsString(
> >                      FacesContext facesContext,
> >                      UIComponent uiComponent,
> >                      Object o) throws ConverterException {
> >                      Long adId = (Long) o;
> >
> >                      if (adId == null || adId.longValue() == 0) {
> >                          return "";
> >                      } else {
> >                          return String.valueOf(adId);
> >                      }
> >                  }
> >              };
> >      }
> >
> >
> >
> >  Thanks
> >
> >  Jason
> >
>

Re: conversion error

Posted by Mike Kienenberger <mk...@gmail.com>.
Instead of

return new ConverterException("Invalid Ad Id");

you need to use

throw new ConverterException("Invalid Ad Id");

On 9/23/05, Jason Nesbitt <ja...@gmail.com> wrote:
> I have an input text field that is bound to a long value in the backing
> bean.  I'm using a custom converter because I want the initial value of the
> input text field to be the empty string and because I want to provide an
> error message that is more specific.  However, when I enter a value in the
> text field that is not a valid long I get the following error.
>  ERROR org.apache.myfaces.el.ValueBindingImpl  - Cannot set
> value for expression '#{login.adId}' to a new value of type
> javax.faces.convert.ConverterException
>  javax.servlet.jsp.el.ELException: Attempt to coerce a
> value of type "javax.faces.convert.ConverterException" to
> type "java.lang.Long"
>
>  What am I doing wrong here? My input text declaration is
>
>  <h:inputText size="12" maxlength="25" value="#{login.adId}"
> converter="#{login.adIdConverter}" />
>
>  and my converter property is
>
>  public Converter getAdIdConverter() {
>          return new Converter() {
>                  public Object getAsObject(
>                      FacesContext facesContext,
>                      UIComponent uiComponent,
>                      String s) throws ConverterException {
>                      try {
>                          return new Long(s);
>                      } catch (NumberFormatException e) {
>                          return new ConverterException("Invalid Ad Id");
>                      }
>                  }
>
>                  public String getAsString(
>                      FacesContext facesContext,
>                      UIComponent uiComponent,
>                      Object o) throws ConverterException {
>                      Long adId = (Long) o;
>
>                      if (adId == null || adId.longValue() == 0) {
>                          return "";
>                      } else {
>                          return String.valueOf(adId);
>                      }
>                  }
>              };
>      }
>
>
>
>  Thanks
>
>  Jason
>