I often want to declare conditional CSS style-classes for components if a certain condition is met. If the condition is not met, then no style should be added. I usually do it like this (which has worked fine so far):
<some:tag styleClass="#{someCondition ? 'SomeClass' : null}" />
Is this OK, or are there any dangerous pitfalls to having null there?
There are no dangerous pitfalls.
#{null}is perfectly fine as EL won’t emit anything to the response (it evaluates basically to an empty string).You also sometimes see this style:
This is the style which is usually used in plain Java code as follows:
In plain Java code converting
nulltoStringwould result in"null", not"", which may be undesireable. For ones not so familiar with EL, the above style is actually more self-documenting. Otherwise you get questions like Doesn’t it print “null” then?