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:[EMAIL PROTECTED]
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 <[EMAIL PROTECTED]> wrote:
Hi Mike,
thanks for this info. Then I have to add my converter manually to every
control :-/
-----Original Message-----
From: Mike Kienenberger [mailto:[EMAIL PROTECTED] <mailto:[EMAIL
PROTECTED]> ]
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 <[EMAIL PROTECTED]> 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
>