Empty strings instead of null values in JSF 2 on Tomcat 7

Recently I was developing a simple application in JSF 2. I moved the application from Glassfish 3.1.2.2 application server to Tomcat 7.0.41 servlet container, migrating the solution from EJB 3.1 to Spring 3.2.4 at the same time. Under Tomcat I used Mojarra 2.1.15 as JSF implementation.

When the application was based on EJB 3.1/Glassfish I worked out a problem with empty strings submitted when user entered no input in a text field on a form on JSF page. In such case I wanted that managed bean received a null value instead of empty String object into its field of String type. My solution consisted of:

  1. Setting javax.faces.INTERPRET_EMPTY_STRING_SUBMITTED_VALUES_AS_NULL to true in web.xml
  2. Using JSF converter which assured empty text input will be returned as null value. This should not be needed but the converter handled text trimming as well to avoid input in form of strings of white spaces

The problem and solution are presented here as well: “h:inputText return a empty string instead of NULL”

After migrating the application to Spring 3/Tomcat 7 I noticed the solution stopped to work. I was getting empty String objects in my managed bean again when user entered no input in text fields. After long investigation it turned out to be a failure in Tomcat. Tomcat has a configuration parameter org.apache.el.parser.COERCE_TO_ZERO. According to the documentation this influence only cases when input from a form field is put into managed bean’s field of numeric type. In theory that was not my case. When this parameter has default value of true, empty input values become zeros.

It turned out that when I set set org.apache.el.parser.COERCE_TO_ZERO to false then my application started to working as before: empty input in form fields were interpreted as null values of managed bean’s fields of String type.

This looks like documentation for Tomcat configuration is misleading or implementation of this parameter in Tomcat 7.0.41 is buggy.

Anyway, here is how to set the parameter for Tomcat in Spring configuration:

<bean id="fixForTomcat"
      class="org.springframework.beans.factory.config.MethodInvokingFactoryBean">
	<property name="targetObject" value="#{@systemProperties}"/>
	<property name="targetMethod" value="setProperty"/>
	<property name="arguments">
		<list>
			<value>org.apache.el.parser.COERCE_TO_ZERO</value>
			<value>false</value>
		</list>
	</property>
</bean>

Of course one can set the parameter in many other ways: providing a command line argument in Tomcat starting script, setting in CATALINA_OPTS environment variable for Tomcat, setting Java system property in the code, etc.

Advertisements

About krzysztoftomaszewski

I've got M.Sc. in software engineering. I graduated in 2005 at Institute of Computer Science, Warsaw University of Technology, Faculty of Electronics and Information Technology. I'm working on computer software design and engineering continuously since 2004.
This entry was posted in Java, JEE, JSF, Spring, Tomcat. Bookmark the permalink.

2 Responses to Empty strings instead of null values in JSF 2 on Tomcat 7

  1. Mauro says:

    This doesn’t seem to work in Tomcat 8 😦

  2. mdiaz967 says:

    thanks

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s