Tomcat’s EL parser tweaking while working with JSF

Did you notice while working with JSF on Tomcat Server, the datatypes misbehave?
We expect Integer to store null value, when nothing in front-end (I mean Textfield is left empty) is entered it but it stores “0”.

Lets try to understand with simple example and solution.

Lets have page named “Form.xhtml” with one input field looks like below.

<h:inputText id="sal" 
             value="#{flowScope.sampleVO.salary}"  
             maxlength="4"/>

And  “SampleVO.java” is simple backing bean.

 public class SampleVO{
   private Integer salary;

   public Integer getSalary(){
    return salary;
   }
   public void setSalary(Integer salary) {
    this.salary = salary;
   }
 }

Now we expect null to be stored when the users left the inputText as null is default value for Integer datatype, but tomcat converts Integer to int and stores “0”. To resolve this we should follow very simple steps.

I will explain under Windows first, later on Unix machine(s).

Windows:

Step 1: Double click <Tomcat installed directory>/bin/Tomcat7w.exe

Step 2: Click on Java Tab, and add Dorg.apache.el.parser.COERCE_TO_ZERO=false  under Java Options and click OK.

Remember before doing this Stop Tomcat Server, add this Parameter then Start.

When your tomcat bin directory doesn’t have Tomcat7w.exe (depends upon installation) and you can see “startup.bat” and “shutdown.bat” files, then follow below steps.

Step 1: Open bin folder in your tomcat installed directory. If you see “setenv.bat” file then simply open it  with notepad and add
set JAVA_OPTS=-Dorg.apache.el.parser.COERCE_TO_ZERO=false in new line and save.

Step 2: If the “setenv.bat” doesn’t exist then create it first and add
set JAVA_OPTS=-Dorg.apache.el.parser.COERCE_TO_ZERO=false in new line

UNIX:

Step 1: Create “setenv.sh” under tomcat installed directory if it doesn’t exists.

Step 2: Add the below line.
     export JAVA_OPTS=”-Dorg.apache.el.parser.COERCE_TO_ZERO=false”

Reference:

https://tomcat.apache.org/tomcat-7.0-doc/config/systemprops.html

Leave a Reply

Your email address will not be published. Required fields are marked *

3 × 4 =