You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@myfaces.apache.org by "Strittmatter, Stephan" <St...@sybit.de> on 2007/03/08 13:36:23 UTC

StringConversion to EMPTY_STRING

Hello all,

I had several times problems with inputText Components which are having
Strings as input.
If there is nothing typed in, the BackingBean get an EMPTY_STRING (""),
but I would expect 
null there? Is this the common behaviour?

For Workaround, I created my own converter adding for every control. Is
there a way to define
it for all String-Controls by default on the orther hand?

Regards,

Stephan

RE: StringConversion to EMPTY_STRING

Posted by "Strittmatter, Stephan" <St...@sybit.de>.
Hello Jeff,

it is Myfaces-1.1.5 I talk about. But it is in Portlet-Envionment
(portlet-bridge).
Could it be that this interferres this also?

Regards,

Stephan 

-----Original Message-----
From: Jeff Bischoff [mailto:jbischoff@klkurz.com] 
Sent: Thursday, March 08, 2007 3:26 PM
To: MyFaces Discussion
Subject: Re: StringConversion to EMPTY_STRING

Stephan,

What versions of MyFaces are we talking about here?

Strittmatter, Stephan wrote:
> Hello all,
> 
> I had several times problems with inputText Components which are
having
> Strings as input.
> If there is nothing typed in, the BackingBean get an EMPTY_STRING
(""),
> but I would expect 
> null there? Is this the common behaviour?
> 
> For Workaround, I created my own converter adding for every control.
Is
> there a way to define
> it for all String-Controls by default on the orther hand?
> 
> Regards,
> 
> Stephan
> 
> 
> 




Re: StringConversion to EMPTY_STRING

Posted by Jeff Bischoff <jb...@klkurz.com>.
Stephan,

What versions of MyFaces are we talking about here?

Strittmatter, Stephan wrote:
> Hello all,
> 
> I had several times problems with inputText Components which are having
> Strings as input.
> If there is nothing typed in, the BackingBean get an EMPTY_STRING (""),
> but I would expect 
> null there? Is this the common behaviour?
> 
> For Workaround, I created my own converter adding for every control. Is
> there a way to define
> it for all String-Controls by default on the orther hand?
> 
> Regards,
> 
> Stephan
> 
> 
> 



Re: StringConversion to EMPTY_STRING

Posted by Jörn Zaefferer <jo...@googlemail.com>.
Ah ok, you add it explicitly to the component. Right, it was mentioned that
you can't specify a converter-by-type in JSF 1.1 for Strings.

On 3/13/07, Strittmatter, Stephan <St...@sybit.de> wrote:
>
> see also now at:
> http://jroller.com/page/stritti?entry=jsf_stringconverter_for_null_values
>
> -----Original Message-----
> From: Strittmatter, Stephan [mailto:Stephan.Strittmatter@sybit.de]
> Sent: Tuesday, March 13, 2007 11:59 AM
> To: MyFaces Discussion
> Subject: RE: StringConversion to EMPTY_STRING
>
> In the faces-config add something like this:
>
> <converter>
>    <converter-id>NullStringConverter</converter-id>
>    <converter-class>foo.bar.NullableStringConverter</converter-class>
> </converter>
>
> Regards, Stephan
>
> ________________________________
>
> From: Jörn Zaefferer [mailto:joern.zaefferer@googlemail.com]
> Sent: Monday, March 12, 2007 4:00 PM
> To: MyFaces Discussion
> Subject: Re: StringConversion to EMPTY_STRING
>
>
> Could you post how you register that converter in the faces-config?
> Thanks.
>
>
> On 3/12/07, Strittmatter, Stephan < Stephan.Strittmatter@sybit.de <mailto:
> Stephan.Strittmatter@sybit.de> > wrote:
>
>         I started this, but then I spreaded this code everywhere in my
> managed beans and it is difficult, when you use a member of a value object
> directly.
>         I created my own "NullableStringConverter" which is doing this Job
> now assigning it in the JSF. It is not just checking for empty String, the
> value has to be null-ed then also because of previous setted values:
>
>         My converter looks like this:
>
>
>           /**
>            * Get the given value as String. In case of an empty String,
> null is returned.
>            *
>            * @param value the value of the control
>            * @param facesContext current facesContext
>            * @param uiComponent the uicomponent providing the value
>            *
>            * @return the given value as String. In case of an empty
> String, null is returned.
>            *
>            * @see javax.faces.convert.Converter#getAsObject (
> javax.faces.context.FacesContext, javax.faces.component.UIComponent,
> java.lang.String)
>            */
>           public Object getAsObject(FacesContext facesContext, UIComponent
> uiComponent, String value) {
>
>             if (facesContext == null) {
>               throw new NullPointerException("facesContext");
>             }
>             if (uiComponent == null) {
>               throw new NullPointerException("uiComponent");
>             }
>
>             return stringToValue(value);
>           }
>
>           /**
>            * Convert the String to value (String or null).
>            *
>            * @param value the string from webcomponent
>            *
>            * @return the object (null if trimmed String is Empty String)
>            */
>           protected Object stringToValue(String value) {
>
>             if (value != null) {
>               value = value.trim();
>               if (value.length() > 0) {
>                 return value + "";
>               }
>             }
>             return null;
>           }
>
>           /**
>            * Convert the value to String for web control.
>            *
>            * @param value the value to be set
>            * @param facesContext current facesContext
>            * @param uiComponent the uicomponent to show the value
>            *
>            * @return the String-converted parameter
>            *
>            * @see javax.faces.convert.Converter#getAsString(
> javax.faces.context.FacesContext, javax.faces.component.UIComponent,
> java.lang.Object)
>            */
>           public String getAsString(FacesContext facesContext, UIComponent
> uiComponent, Object value) {
>
>             if (facesContext == null) {
>               throw new NullPointerException("facesContext");
>             }
>             if (uiComponent == null) {
>               throw new NullPointerException("uiComponent");
>             }
>             return valueToString(value);
>           }
>
>           /**
>            * Converts the value to HTMLized String.
>            *
>            * @param value the object to be converted
>            *
>            * @return String representation
>            */
>           protected String valueToString(Object value) {
>
>             if (value == null) {
>               return "";
>             }
>             if (value instanceof String) {
>               return (String) value;
>             }
>             return value + "";
>           }
>
>         ________________________________
>
>         From: Jörn Zaefferer [mailto:joern.zaefferer@googlemail.com]
>         Sent: Monday, March 12, 2007 9:29 AM
>         To: MyFaces Discussion
>         Subject: Re: StringConversion to EMPTY_STRING
>
>
>         You could add a check for the value to set in each modifier,
> something like this:
>
>         public void setName(String newName) {
>           if ( !"".equasl(newName) ) {
>             name = newName;
>           }
>         }
>
>         Still quite cumbersome...
>
>
>         On 3/12/07, Strittmatter, Stephan <St...@sybit.de>
> wrote:
>
>                 Hi Mike,
>
>                 thanks for this info. Then I have to add my converter
> manually to every
>                 control :-/
>
>
>                 -----Original Message-----
>                 From: Mike Kienenberger [mailto:mkienenb@gmail.com<mailto:
> mkienenb@gmail.com > ]
>                 Sent: Thursday, March 08, 2007 7:42 PM
>                 To: MyFaces Discussion
>                 Subject: Re: StringConversion to EMPTY_STRING
>
>                 The JSF spec says that components will return the empty
> string, not
>                 null.
>
>                 There is no way to specify a converter-by-type for String
> in JSF 1.1.
>                 You can specify String converters for JSF 1.2.
>
>
> https://javaserverfaces-spec-public.dev.java.net/issues/show_bug.cgi?id= <
> https://javaserverfaces-spec-public.dev.java.net/issues/show_bug.cgi?id=>
>                 131
>
>                 On 3/8/07, Strittmatter, Stephan <
> Stephan.Strittmatter@sybit.de > wrote:
>                 > Hello all,
>                 >
>                 > I had several times problems with inputText Components
> which are
>                 having
>                 > Strings as input.
>                 > If there is nothing typed in, the BackingBean get an
> EMPTY_STRING
>                 (""),
>                 > but I would expect
>                 > null there? Is this the common behaviour?
>                 >
>                 > For Workaround, I created my own converter adding for
> every control.
>                 Is
>                 > there a way to define
>                 > it for all String-Controls by default on the orther
> hand?
>                 >
>                 > Regards,
>                 >
>                 > Stephan
>                 >
>
>
>
>
>
>
>
>
>

RE: StringConversion to EMPTY_STRING

Posted by "Strittmatter, Stephan" <St...@sybit.de>.
see also now at: http://jroller.com/page/stritti?entry=jsf_stringconverter_for_null_values 

-----Original Message-----
From: Strittmatter, Stephan [mailto:Stephan.Strittmatter@sybit.de] 
Sent: Tuesday, March 13, 2007 11:59 AM
To: MyFaces Discussion
Subject: RE: StringConversion to EMPTY_STRING

In the faces-config add something like this:
 
<converter>
   <converter-id>NullStringConverter</converter-id>
   <converter-class>foo.bar.NullableStringConverter</converter-class>
</converter>

Regards, Stephan

________________________________

From: Jörn Zaefferer [mailto:joern.zaefferer@googlemail.com] 
Sent: Monday, March 12, 2007 4:00 PM
To: MyFaces Discussion
Subject: Re: StringConversion to EMPTY_STRING


Could you post how you register that converter in the faces-config? Thanks.


On 3/12/07, Strittmatter, Stephan < Stephan.Strittmatter@sybit.de <ma...@sybit.de> > wrote: 

	I started this, but then I spreaded this code everywhere in my managed beans and it is difficult, when you use a member of a value object directly. 
	I created my own "NullableStringConverter" which is doing this Job now assigning it in the JSF. It is not just checking for empty String, the value has to be null-ed then also because of previous setted values: 
	
	My converter looks like this:
	
	
	  /**
	   * Get the given value as String. In case of an empty String, null is returned.
	   *
	   * @param value the value of the control
	   * @param facesContext current facesContext 
	   * @param uiComponent the uicomponent providing the value
	   *
	   * @return the given value as String. In case of an empty String, null is returned.
	   *
	   * @see javax.faces.convert.Converter#getAsObject (javax.faces.context.FacesContext, javax.faces.component.UIComponent, java.lang.String)
	   */
	  public Object getAsObject(FacesContext facesContext, UIComponent uiComponent, String value) {
	
	    if (facesContext == null) { 
	      throw new NullPointerException("facesContext");
	    }
	    if (uiComponent == null) {
	      throw new NullPointerException("uiComponent");
	    }
	
	    return stringToValue(value); 
	  }
	
	  /**
	   * Convert the String to value (String or null).
	   *
	   * @param value the string from webcomponent
	   *
	   * @return the object (null if trimmed String is Empty String)
	   */
	  protected Object stringToValue(String value) {
	
	    if (value != null) {
	      value = value.trim();
	      if (value.length() > 0) {
	        return value + "";
	      }
	    }
	    return null; 
	  }
	
	  /**
	   * Convert the value to String for web control.
	   *
	   * @param value the value to be set
	   * @param facesContext current facesContext
	   * @param uiComponent the uicomponent to show the value 
	   *
	   * @return the String-converted parameter
	   *
	   * @see javax.faces.convert.Converter#getAsString(javax.faces.context.FacesContext, javax.faces.component.UIComponent, java.lang.Object)
	   */
	  public String getAsString(FacesContext facesContext, UIComponent uiComponent, Object value) { 
	
	    if (facesContext == null) {
	      throw new NullPointerException("facesContext");
	    }
	    if (uiComponent == null) {
	      throw new NullPointerException("uiComponent");
	    } 
	    return valueToString(value);
	  }
	
	  /**
	   * Converts the value to HTMLized String.
	   *
	   * @param value the object to be converted
	   *
	   * @return String representation
	   */
	  protected String valueToString(Object value) { 
	
	    if (value == null) {
	      return "";
	    }
	    if (value instanceof String) {
	      return (String) value;
	    }
	    return value + "";
	  }
	
	________________________________ 
	
	From: Jörn Zaefferer [mailto:joern.zaefferer@googlemail.com]
	Sent: Monday, March 12, 2007 9:29 AM
	To: MyFaces Discussion
	Subject: Re: StringConversion to EMPTY_STRING 
	
	
	You could add a check for the value to set in each modifier, something like this:
	
	public void setName(String newName) {
	  if ( !"".equasl(newName) ) {
	    name = newName;
	  }
	}
	
	Still quite cumbersome...
	
	
	On 3/12/07, Strittmatter, Stephan <St...@sybit.de> wrote:
	
	        Hi Mike,
	
	        thanks for this info. Then I have to add my converter manually to every 
	        control :-/
	
	
	        -----Original Message-----
	        From: Mike Kienenberger [mailto:mkienenb@gmail.com <mailto:mkienenb@gmail.com > ]
	        Sent: Thursday, March 08, 2007 7:42 PM
	        To: MyFaces Discussion
	        Subject: Re: StringConversion to EMPTY_STRING
	
	        The JSF spec says that components will return the empty string, not 
	        null.
	
	        There is no way to specify a converter-by-type for String in JSF 1.1.
	        You can specify String converters for JSF 1.2.
	
	         https://javaserverfaces-spec-public.dev.java.net/issues/show_bug.cgi?id= <https://javaserverfaces-spec-public.dev.java.net/issues/show_bug.cgi?id=> 
	        131
	
	        On 3/8/07, Strittmatter, Stephan <Stephan.Strittmatter@sybit.de > wrote:
	        > Hello all,
	        >
	        > I had several times problems with inputText Components which are
	        having
	        > Strings as input.
	        > If there is nothing typed in, the BackingBean get an EMPTY_STRING 
	        (""),
	        > but I would expect
	        > null there? Is this the common behaviour?
	        >
	        > For Workaround, I created my own converter adding for every control. 
	        Is
	        > there a way to define
	        > it for all String-Controls by default on the orther hand?
	        >
	        > Regards,
	        >
	        > Stephan
	        > 
	
	
	
	
	




RE: StringConversion to EMPTY_STRING

Posted by "Strittmatter, Stephan" <St...@sybit.de>.
In the faces-config add something like this:
 
<converter>
   <converter-id>NullStringConverter</converter-id>
   <converter-class>foo.bar.NullableStringConverter</converter-class>
</converter>

Regards, Stephan

________________________________

From: Jörn Zaefferer [mailto:joern.zaefferer@googlemail.com] 
Sent: Monday, March 12, 2007 4:00 PM
To: MyFaces Discussion
Subject: Re: StringConversion to EMPTY_STRING


Could you post how you register that converter in the faces-config? Thanks.


On 3/12/07, Strittmatter, Stephan < Stephan.Strittmatter@sybit.de <ma...@sybit.de> > wrote: 

	I started this, but then I spreaded this code everywhere in my managed beans and it is difficult, when you use a member of a value object directly. 
	I created my own "NullableStringConverter" which is doing this Job now assigning it in the JSF. It is not just checking for empty String, the value has to be null-ed then also because of previous setted values: 
	
	My converter looks like this:
	
	
	  /**
	   * Get the given value as String. In case of an empty String, null is returned.
	   *
	   * @param value the value of the control
	   * @param facesContext current facesContext 
	   * @param uiComponent the uicomponent providing the value
	   *
	   * @return the given value as String. In case of an empty String, null is returned.
	   *
	   * @see javax.faces.convert.Converter#getAsObject (javax.faces.context.FacesContext, javax.faces.component.UIComponent, java.lang.String)
	   */
	  public Object getAsObject(FacesContext facesContext, UIComponent uiComponent, String value) {
	
	    if (facesContext == null) { 
	      throw new NullPointerException("facesContext");
	    }
	    if (uiComponent == null) {
	      throw new NullPointerException("uiComponent");
	    }
	
	    return stringToValue(value); 
	  }
	
	  /**
	   * Convert the String to value (String or null).
	   *
	   * @param value the string from webcomponent
	   *
	   * @return the object (null if trimmed String is Empty String)
	   */
	  protected Object stringToValue(String value) {
	
	    if (value != null) {
	      value = value.trim();
	      if (value.length() > 0) {
	        return value + "";
	      }
	    }
	    return null; 
	  }
	
	  /**
	   * Convert the value to String for web control.
	   *
	   * @param value the value to be set
	   * @param facesContext current facesContext
	   * @param uiComponent the uicomponent to show the value 
	   *
	   * @return the String-converted parameter
	   *
	   * @see javax.faces.convert.Converter#getAsString(javax.faces.context.FacesContext, javax.faces.component.UIComponent, java.lang.Object)
	   */
	  public String getAsString(FacesContext facesContext, UIComponent uiComponent, Object value) { 
	
	    if (facesContext == null) {
	      throw new NullPointerException("facesContext");
	    }
	    if (uiComponent == null) {
	      throw new NullPointerException("uiComponent");
	    } 
	    return valueToString(value);
	  }
	
	  /**
	   * Converts the value to HTMLized String.
	   *
	   * @param value the object to be converted
	   *
	   * @return String representation
	   */
	  protected String valueToString(Object value) { 
	
	    if (value == null) {
	      return "";
	    }
	    if (value instanceof String) {
	      return (String) value;
	    }
	    return value + "";
	  }
	
	________________________________ 
	
	From: Jörn Zaefferer [mailto:joern.zaefferer@googlemail.com]
	Sent: Monday, March 12, 2007 9:29 AM
	To: MyFaces Discussion
	Subject: Re: StringConversion to EMPTY_STRING 
	
	
	You could add a check for the value to set in each modifier, something like this:
	
	public void setName(String newName) {
	  if ( !"".equasl(newName) ) {
	    name = newName;
	  }
	}
	
	Still quite cumbersome...
	
	
	On 3/12/07, Strittmatter, Stephan <St...@sybit.de> wrote:
	
	        Hi Mike,
	
	        thanks for this info. Then I have to add my converter manually to every 
	        control :-/
	
	
	        -----Original Message-----
	        From: Mike Kienenberger [mailto:mkienenb@gmail.com <mailto:mkienenb@gmail.com > ]
	        Sent: Thursday, March 08, 2007 7:42 PM
	        To: MyFaces Discussion
	        Subject: Re: StringConversion to EMPTY_STRING
	
	        The JSF spec says that components will return the empty string, not 
	        null.
	
	        There is no way to specify a converter-by-type for String in JSF 1.1.
	        You can specify String converters for JSF 1.2.
	
	         https://javaserverfaces-spec-public.dev.java.net/issues/show_bug.cgi?id= <https://javaserverfaces-spec-public.dev.java.net/issues/show_bug.cgi?id=> 
	        131
	
	        On 3/8/07, Strittmatter, Stephan <Stephan.Strittmatter@sybit.de > wrote:
	        > Hello all,
	        >
	        > I had several times problems with inputText Components which are
	        having
	        > Strings as input.
	        > If there is nothing typed in, the BackingBean get an EMPTY_STRING 
	        (""),
	        > but I would expect
	        > null there? Is this the common behaviour?
	        >
	        > For Workaround, I created my own converter adding for every control. 
	        Is
	        > there a way to define
	        > it for all String-Controls by default on the orther hand?
	        >
	        > Regards,
	        >
	        > Stephan
	        > 
	
	
	
	
	



Re: StringConversion to EMPTY_STRING

Posted by Jörn Zaefferer <jo...@googlemail.com>.
Could you post how you register that converter in the faces-config? Thanks.

On 3/12/07, Strittmatter, Stephan <St...@sybit.de> wrote:
>
> I started this, but then I spreaded this code everywhere in my managed
> beans and it is difficult, when you use a member of a value object directly.
> I created my own "NullableStringConverter" which is doing this Job now
> assigning it in the JSF. It is not just checking for empty String, the value
> has to be null-ed then also because of previous setted values:
>
> My converter looks like this:
>
>
>   /**
>    * Get the given value as String. In case of an empty String, null is
> returned.
>    *
>    * @param value the value of the control
>    * @param facesContext current facesContext
>    * @param uiComponent the uicomponent providing the value
>    *
>    * @return the given value as String. In case of an empty String, null
> is returned.
>    *
>    * @see javax.faces.convert.Converter#getAsObject(
> javax.faces.context.FacesContext, javax.faces.component.UIComponent,
> java.lang.String)
>    */
>   public Object getAsObject(FacesContext facesContext, UIComponent
> uiComponent, String value) {
>
>     if (facesContext == null) {
>       throw new NullPointerException("facesContext");
>     }
>     if (uiComponent == null) {
>       throw new NullPointerException("uiComponent");
>     }
>
>     return stringToValue(value);
>   }
>
>   /**
>    * Convert the String to value (String or null).
>    *
>    * @param value the string from webcomponent
>    *
>    * @return the object (null if trimmed String is Empty String)
>    */
>   protected Object stringToValue(String value) {
>
>     if (value != null) {
>       value = value.trim();
>       if (value.length() > 0) {
>         return value + "";
>       }
>     }
>     return null;
>   }
>
>   /**
>    * Convert the value to String for web control.
>    *
>    * @param value the value to be set
>    * @param facesContext current facesContext
>    * @param uiComponent the uicomponent to show the value
>    *
>    * @return the String-converted parameter
>    *
>    * @see javax.faces.convert.Converter#getAsString(
> javax.faces.context.FacesContext, javax.faces.component.UIComponent,
> java.lang.Object)
>    */
>   public String getAsString(FacesContext facesContext, UIComponent
> uiComponent, Object value) {
>
>     if (facesContext == null) {
>       throw new NullPointerException("facesContext");
>     }
>     if (uiComponent == null) {
>       throw new NullPointerException("uiComponent");
>     }
>     return valueToString(value);
>   }
>
>   /**
>    * Converts the value to HTMLized String.
>    *
>    * @param value the object to be converted
>    *
>    * @return String representation
>    */
>   protected String valueToString(Object value) {
>
>     if (value == null) {
>       return "";
>     }
>     if (value instanceof String) {
>       return (String) value;
>     }
>     return value + "";
>   }
>
> ________________________________
>
> From: Jörn Zaefferer [mailto:joern.zaefferer@googlemail.com]
> Sent: Monday, March 12, 2007 9:29 AM
> To: MyFaces Discussion
> Subject: Re: StringConversion to EMPTY_STRING
>
>
> You could add a check for the value to set in each modifier, something
> like this:
>
> public void setName(String newName) {
>   if ( !"".equasl(newName) ) {
>     name = newName;
>   }
> }
>
> Still quite cumbersome...
>
>
> On 3/12/07, Strittmatter, Stephan <St...@sybit.de> wrote:
>
>         Hi Mike,
>
>         thanks for this info. Then I have to add my converter manually to
> every
>         control :-/
>
>
>         -----Original Message-----
>         From: Mike Kienenberger [mailto:mkienenb@gmail.com <mailto:
> mkienenb@gmail.com> ]
>         Sent: Thursday, March 08, 2007 7:42 PM
>         To: MyFaces Discussion
>         Subject: Re: StringConversion to EMPTY_STRING
>
>         The JSF spec says that components will return the empty string,
> not
>         null.
>
>         There is no way to specify a converter-by-type for String in JSF
> 1.1.
>         You can specify String converters for JSF 1.2.
>
>
> https://javaserverfaces-spec-public.dev.java.net/issues/show_bug.cgi?id=
>         131
>
>         On 3/8/07, Strittmatter, Stephan <St...@sybit.de>
> wrote:
>         > Hello all,
>         >
>         > I had several times problems with inputText Components which are
>         having
>         > Strings as input.
>         > If there is nothing typed in, the BackingBean get an
> EMPTY_STRING
>         (""),
>         > but I would expect
>         > null there? Is this the common behaviour?
>         >
>         > For Workaround, I created my own converter adding for every
> control.
>         Is
>         > there a way to define
>         > it for all String-Controls by default on the orther hand?
>         >
>         > Regards,
>         >
>         > Stephan
>         >
>
>
>
>
>

RE: StringConversion to EMPTY_STRING

Posted by "Strittmatter, Stephan" <St...@sybit.de>.
I started this, but then I spreaded this code everywhere in my managed beans and it is difficult, when you use a member of a value object directly.
I created my own "NullableStringConverter" which is doing this Job now assigning it in the JSF. It is not just checking for empty String, the value has to be null-ed then also because of previous setted values:
 
My converter looks like this:
 

  /**
   * Get the given value as String. In case of an empty String, null is returned.
   * 
   * @param value the value of the control
   * @param facesContext current facesContext
   * @param uiComponent the uicomponent providing the value
   * 
   * @return the given value as String. In case of an empty String, null is returned.
   * 
   * @see javax.faces.convert.Converter#getAsObject(javax.faces.context.FacesContext, javax.faces.component.UIComponent, java.lang.String)
   */
  public Object getAsObject(FacesContext facesContext, UIComponent uiComponent, String value) {

    if (facesContext == null) {
      throw new NullPointerException("facesContext");
    }
    if (uiComponent == null) {
      throw new NullPointerException("uiComponent");
    }

    return stringToValue(value);
  }

  /**
   * Convert the String to value (String or null).
   * 
   * @param value the string from webcomponent
   * 
   * @return the object (null if trimmed String is Empty String)
   */
  protected Object stringToValue(String value) {

    if (value != null) {
      value = value.trim();
      if (value.length() > 0) {
        return value + "";
      }
    }
    return null;
  }

  /**
   * Convert the value to String for web control.
   * 
   * @param value the value to be set
   * @param facesContext current facesContext
   * @param uiComponent the uicomponent to show the value
   * 
   * @return the String-converted parameter
   * 
   * @see javax.faces.convert.Converter#getAsString(javax.faces.context.FacesContext, javax.faces.component.UIComponent, java.lang.Object)
   */
  public String getAsString(FacesContext facesContext, UIComponent uiComponent, Object value) {

    if (facesContext == null) {
      throw new NullPointerException("facesContext");
    }
    if (uiComponent == null) {
      throw new NullPointerException("uiComponent");
    }
    return valueToString(value);
  }

  /**
   * Converts the value to HTMLized String.
   * 
   * @param value the object to be converted
   * 
   * @return String representation
   */
  protected String valueToString(Object value) {

    if (value == null) {
      return "";
    }
    if (value instanceof String) {
      return (String) value;
    }
    return value + "";
  }

________________________________

From: Jörn Zaefferer [mailto:joern.zaefferer@googlemail.com] 
Sent: Monday, March 12, 2007 9:29 AM
To: MyFaces Discussion
Subject: Re: StringConversion to EMPTY_STRING


You could add a check for the value to set in each modifier, something like this:

public void setName(String newName) {
  if ( !"".equasl(newName) ) {
    name = newName;
  }
}

Still quite cumbersome... 


On 3/12/07, Strittmatter, Stephan <St...@sybit.de> wrote: 

	Hi Mike,
	
	thanks for this info. Then I have to add my converter manually to every
	control :-/
	
	
	-----Original Message-----
	From: Mike Kienenberger [mailto:mkienenb@gmail.com <ma...@gmail.com> ]
	Sent: Thursday, March 08, 2007 7:42 PM
	To: MyFaces Discussion
	Subject: Re: StringConversion to EMPTY_STRING
	
	The JSF spec says that components will return the empty string, not
	null.
	
	There is no way to specify a converter-by-type for String in JSF 1.1.
	You can specify String converters for JSF 1.2.
	
	https://javaserverfaces-spec-public.dev.java.net/issues/show_bug.cgi?id= 
	131
	
	On 3/8/07, Strittmatter, Stephan <St...@sybit.de> wrote:
	> Hello all,
	>
	> I had several times problems with inputText Components which are 
	having
	> Strings as input.
	> If there is nothing typed in, the BackingBean get an EMPTY_STRING
	(""),
	> but I would expect
	> null there? Is this the common behaviour?
	>
	> For Workaround, I created my own converter adding for every control. 
	Is
	> there a way to define
	> it for all String-Controls by default on the orther hand?
	>
	> Regards,
	>
	> Stephan
	>
	
	



Re: StringConversion to EMPTY_STRING

Posted by Jörn Zaefferer <jo...@googlemail.com>.
You could add a check for the value to set in each modifier, something like
this:

public void setName(String newName) {
  if ( !"".equasl(newName) ) {
    name = newName;
  }
}

Still quite cumbersome...

On 3/12/07, Strittmatter, Stephan <St...@sybit.de> wrote:
>
> Hi Mike,
>
> thanks for this info. Then I have to add my converter manually to every
> control :-/
>
>
> -----Original Message-----
> From: Mike Kienenberger [mailto:mkienenb@gmail.com]
> Sent: Thursday, March 08, 2007 7:42 PM
> To: MyFaces Discussion
> Subject: Re: StringConversion to EMPTY_STRING
>
> The JSF spec says that components will return the empty string, not
> null.
>
> There is no way to specify a converter-by-type for String in JSF 1.1.
> You can specify String converters for JSF 1.2.
>
> https://javaserverfaces-spec-public.dev.java.net/issues/show_bug.cgi?id=
> 131
>
> On 3/8/07, Strittmatter, Stephan <St...@sybit.de> wrote:
> > Hello all,
> >
> > I had several times problems with inputText Components which are
> having
> > Strings as input.
> > If there is nothing typed in, the BackingBean get an EMPTY_STRING
> (""),
> > but I would expect
> > null there? Is this the common behaviour?
> >
> > For Workaround, I created my own converter adding for every control.
> Is
> > there a way to define
> > it for all String-Controls by default on the orther hand?
> >
> > Regards,
> >
> > Stephan
> >
>
>

RE: StringConversion to EMPTY_STRING

Posted by "Strittmatter, Stephan" <St...@sybit.de>.
Hi Mike,

thanks for this info. Then I have to add my converter manually to every
control :-/


-----Original Message-----
From: Mike Kienenberger [mailto:mkienenb@gmail.com] 
Sent: Thursday, March 08, 2007 7:42 PM
To: MyFaces Discussion
Subject: Re: StringConversion to EMPTY_STRING

The JSF spec says that components will return the empty string, not
null.

There is no way to specify a converter-by-type for String in JSF 1.1.
You can specify String converters for JSF 1.2.

https://javaserverfaces-spec-public.dev.java.net/issues/show_bug.cgi?id=
131

On 3/8/07, Strittmatter, Stephan <St...@sybit.de> wrote:
> Hello all,
>
> I had several times problems with inputText Components which are
having
> Strings as input.
> If there is nothing typed in, the BackingBean get an EMPTY_STRING
(""),
> but I would expect
> null there? Is this the common behaviour?
>
> For Workaround, I created my own converter adding for every control.
Is
> there a way to define
> it for all String-Controls by default on the orther hand?
>
> Regards,
>
> Stephan
>


Re: StringConversion to EMPTY_STRING

Posted by Mike Kienenberger <mk...@gmail.com>.
The JSF spec says that components will return the empty string, not null.

There is no way to specify a converter-by-type for String in JSF 1.1.
You can specify String converters for JSF 1.2.

https://javaserverfaces-spec-public.dev.java.net/issues/show_bug.cgi?id=131

On 3/8/07, Strittmatter, Stephan <St...@sybit.de> wrote:
> Hello all,
>
> I had several times problems with inputText Components which are having
> Strings as input.
> If there is nothing typed in, the BackingBean get an EMPTY_STRING (""),
> but I would expect
> null there? Is this the common behaviour?
>
> For Workaround, I created my own converter adding for every control. Is
> there a way to define
> it for all String-Controls by default on the orther hand?
>
> Regards,
>
> Stephan
>