JSF EL string concatenation
November 12, 2011 21:03:03 Last update: November 12, 2011 21:03:03
Experts may disagree but I found it absolutely stunning that JSF EL does not provide an operator for string concatenation. The Java "+" operator is there for the take. Java, which is a strongly typed compiled language, overloads the "+" operator in such a way that any object can be concatenated with a string. But JSF EL, which definitely isn't as strongly typed as Java, restricts the "+" operator to numerical values only! Of course, experts may argue that the "+" operator overloading is a huge design flaw of the Java language. But even so, JSF EL is not the right place to fix it!
In some cases, a concatenation operator isn't needed, for example:
But in case the concatenated string is the key to a map, or an argument to a function call, you are out of luck. The following definitely doesn't work:
Putting a "+" between
As a workaround, you can define an intermediate variable:
In some cases, a concatenation operator isn't needed, for example:
<ui:repeat var="tab" value="#{tabs}"> <img src="/path/to/images/#{tab}.png"/> </ui:repeat>
But in case the concatenated string is the key to a map, or an argument to a function call, you are out of luck. The following definitely doesn't work:
<ui:repeat var="tab" value="#{tabs}"> <img src="#{imgdict['/homepage/tabs/'tab]}"/> </ui:repeat>
Putting a "+" between
/homepage/tabs/ and tab leads you to the error that /homepage/tabs/ isn't a numerical value.
As a workaround, you can define an intermediate variable:
<ui:repeat var="tab" value="#{tabs}"> <ui:param name="throwaway" value="/homepage/tabs/#{tab}"/> <img src="#{imgdict[throwaway]}"/> </ui:repeat>