I have a JSF 2.0 application on Tomcat with many <h:inputText>
fields to input data in my database. Some fields are not required.
<h:inputText value="#{registerBean.user.phoneNumber}" id="phoneNumber">
<f:validateLength maximum="20" />
</h:inputText>
When the user leave this field empty JSF sets empty string ""
instead of null
.
How can I fix this behavior without checking every String with
if (string.equals("")) { string = null; }
You can configure JSF 2.x to interpret empty submitted values as null by the following
context-param
inweb.xml
(which has a pretty long name, that'll also be why I couldn't recall it ;) ):For reference and for ones who are interested, in JSF 1.2 (and thus not 1.1 or older because it's by design not possible to have a
Converter
forjava.lang.String
) this is workaroundable with the followingConverter
:...which needs to be registered in
faces-config.xml
as follows:In case you're not on Java 6 yet, replace
submittedValue.empty()
bysubmittedValue.length() == 0
.See also
i hope this is the right way to say that i can't find a solution for the Problem.
I have add the
context-param
to myweb.xml
but it has no result. I use a Tomcat 6.0.24 Server with this twocontext-param: javax.faces.PROJECT_STAGE =
Developmentjavax.faces.VALIDATE_EMPTY_FIELDS = true