Setting a value to null with EL

Beginning with Expression Language (EL) 2.2 it is possible to call method with arguments. I feel that most of the time it is generally a good idea to try not to use this feature because it breaks the KISS principal but when you need it it can be a life saver.

I’ve recently been doing some work on the PlotFaces demonstration site and I needed to be able set the parameters to null. Since I have literally hundreds of parameters I didn’t want to have to provide setter / getter methods for all of them (in fact with axes this wouldn’t be practical as I don’t know how many there will be). With PlotFaces a value of null means use the default for that setting, a useful feature for keeping the size of the JavaScript output to a minimum.

<p:commandbutton action="#{bean.setFoo( true )}" value="Bar">

<p:commandbutton value="Baz">
    <f:setpropertyactionlistener target="#{bean.foo}" value="#{null}"/>
</p:commandbutton>

The problem I had was trying to set a value to null. While the first command button is fine for setting the foo property to true or false using that syntax with null resulted in the value being set to false. The second syntax, however, sets the foo property to null as expected. My guess is that it works because in the second case you are trying to supply an object but in the first case the system is assuming it’s going to recieve a primative.